Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with simple compare condition?

Reply
Thread Tools

Help with simple compare condition?

 
 
bobmct
Guest
Posts: n/a
 
      08-08-2009
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
 
Reply With Quote
 
 
 
 
Doug Miller
Guest
Posts: n/a
 
      08-08-2009
In article <>, bobmct <> wrote:
>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.
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-09-2009
On Sat, 08 Aug 2009 14:28:52 -0400, bobmct <> wrote:

>On Sat, 08 Aug 2009 12:53:48 -0500, Tad J McClellan
><> wrote:
>
>presence as in non-empty and non-space.
>
>My test is: if (($VarA and !$VarB) or (!$VarA and $VarB)) { ...
>
>
>>bobmct <> wrote:
>>
>>> I need to compare two variables for presence of content to meet the
>>> following tests:

>>
>>
>>What does "presence of content" mean when you say it?
>>
>>If "content" is "foo", then is it "present" in
>>
>> foo
>> foobar
>> barfoo
>> barfoobaz
>>
>>?
>>
>>> 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

>>
>>
>>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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple question: compare words Ahmet Kilic Ruby 6 09-18-2009 08:07 AM
lwp::simple and rget--can I compare bdy Perl Misc 4 08-05-2009 06:20 PM
Simple date compare Ulf Java 12 03-11-2008 02:23 PM
Simple simple program error...please help tasheeta@gmail.com C++ 14 11-02-2005 12:52 PM
2 tables, compare data help?? =?Utf-8?B?TWFyaw==?= ASP .Net 1 01-16-2004 03:33 PM



Advertisments