Help with simple compare condition?

B

bobmct

Fellow mongers;

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
 
D

Doug Miller

Fellow mongers;

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

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)
-or-

As I more simply thought of if:

VarA ne VarB then true

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.
Each Var could contain

What if the variable doesn't exist?
1) an empty string

Is that "present", or not?
2) a string value

Presumably, that's "present" -- but what if its value is entirely whitespace
characters (blank, tab, line feed, etc.)?
3) a numeric value

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?
4) a space

Is that "present", or not?
I cannot seem to get a consistent result from the various methods I've
used.

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".
Any recommendations or suggestions greatly appreciated.

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

sln

On Sat, 08 Aug 2009 12:53:48 -0500, Tad J McClellan

presence as in non-empty and non-space.

My test is: if (($VarA and !$VarB) or (!$VarA and $VarB)) { ...

"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";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top