Help with simple compare condition?

Discussion in 'Perl Misc' started by bobmct, Aug 8, 2009.

  1. bobmct

    bobmct Guest

    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
    bobmct, Aug 8, 2009
    #1
    1. Advertising

  2. bobmct

    Doug Miller Guest

    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.
    Doug Miller, Aug 8, 2009
    #2
    1. Advertising

  3. bobmct

    Guest

    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";
    }
    , Aug 9, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    675
    Remon van Vliet
    Jun 15, 2005
  2. Pawel
    Replies:
    1
    Views:
    1,415
    Ian Collins
    Oct 26, 2006
  3. bpascal123
    Replies:
    6
    Views:
    331
    Keith Thompson
    Jun 6, 2011
  4. papoupapa

    (simple) condition within for loop

    papoupapa, Jun 12, 2011, in forum: C Programming
    Replies:
    12
    Views:
    674
    Tim Rentsch
    Jun 14, 2011
  5. Bill W.
    Replies:
    13
    Views:
    277
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page