#### Do you have a question? Post it now! No Registration Necessary. Now with pictures!

**posted on**

August 8, 2009, 4:47 pm

I've done research in my many perl books as well as numerous perl

references on the web and still am confused on this rather simple

situation (embarrassingly so):

I need to compare two variables for presence of content to meet the

following tests:

VarA = present and VarB = present then false

VarA = empty and VarB = empty then false

VarA = present and VarB = empty then true

VarA = empty and VarB = present then true

-or-

As I more simply thought of if:

VarA ne VarB then true

Each Var could contain

1) an empty string

2) a string value

3) a numeric value

4) a space

I cannot seem to get a consistent result from the various methods I've

used.

Any recommendations or suggestions greatly appreciated.

Thanks - B

## Re: Help with simple compare condition?

What does "presence of content" mean when you say it?

If "content" is "foo", then is it "present" in

foo

foobar

barfoo

barfoobaz

?

That is the truth table for the xor operator, so I'd start with that.

You have only 4 cases, if you wrote a short and complete program

that shows VarA and VarB for the 4 cases, then we could probably

help you solve your problem.

--

Tad McClellan

email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: Help with simple compare condition?

"non-empty and non-space"

Would this be TRUE and all others be FALSE?

...

Logic conditions:

VarA = present and VarB = present then false

VarA = empty and VarB = empty then false

VarA = present and VarB = empty then true

VarA = empty and VarB = present then true

A && B false

!A && !B false

A && !B true

!A && B true

Convert to 'true'/logical or:

#1 !(!A || !B) true logical

#2 !( A || B) true logical

#3 !A || B true A xor B

#4 A || !B true A xor B

The xor operator can cover #3#4 in once statement because its atomic.

(Xor = only one can/must be true)

#5 (from #3#2) A xor B true

The 'truth' breaks down when you consider #1#2 in relation to each other

and #5.

For instance A xor B is not necessarily equal to !(!A || !B)) nor !( A || B).

You have to ask yourself "what is the truth I am actually looking for?"

Not all 'truths' are equal. Some are falshood in disguise.

Truth as a whole is often confused with 'truth' as a subset.

The absolute truth MUST be a combination of all incremental truth.

Your attempt at logic is pffft..

You should attempt to take a junior college course called 'Formal Symbolic

Logic',

something that will help you immensly.

-sln

---------------------------------

use strict;

use warnings;

my ($VarA,$VarB,$A,$B);

$VarA = 'asg';

$VarB = '';

$A = defined($VarA) && $VarA =~ /^\s*.+/ ? 1:0;

$B = defined($VarB) && $VarB =~ /^\s*.+/ ? 1:0;

print "$A $B\n";

if ( ($A xor $B) ) {

print "Xor condition is true\n";

} else {

print "Xor cndition is false\n";

}

if ( (!(!$A || !$B)) && !($A || $B) ) {

print "Logical condition is true\n";

} else {

print "Logical cndition is false\n";

}

## Re: Help with simple compare condition?

I think you need a more precise definition of "present". Do you mean:

a) the variable exists

b) the variable exists and has a value (e.g. non-empty)

c) the variable exists and has a

***specific***value (e.g. non-space if a string,

non-zero if a number)

That, of course, is an entirely different condition. Two different non-empty

strings are "present", but not equal. The numbers 1 and 2 are present, but,

again, not equal. Moreover, the numbers 1 and 1 are present, and

***are***equal.

What if the variable doesn't exist?

Is that "present", or not?

Presumably, that's "present" -- but what if its value is entirely whitespace

characters (blank, tab, line feed, etc.)?

Presumably, that's "present" too -- but what if the value is zero? Is that

"present"? Do you distinguish between integer zero and floating-point

zero-point-zero?

Is that "present", or not?

Perhaps if you posted some of those methods...?

I suspect that the largest part of your trouble is a failure to define clearly

exactly what you mean by "present".

Sure -- show us what you've tried, and what happens when you try it.

#### Site Timeline

- » capturing stdout and stderr?
- — Next thread in » PERL Discussions

- » DBI query does not fetch all data
- — Previous thread in » PERL Discussions

- » s suffix question
- — Newest thread in » PERL Discussions

- » Dell Battery Slice LED codes
- — The site's Newest Thread. Posted in » Laptop Computers Forum