Re: very strange conditional problem

Discussion in 'Perl' started by J. Romano, Feb 21, 2004.

  1. J. Romano

    J. Romano Guest

    Kodeguru <> wrote in message news:<2SGZb.27067$>...
    > ** I also posted this in comp.lang.perl **
    >
    > # this line prints $equip_row->[16] is ()\n
    > # which is right
    > print "\$equip_row->[16] is ($equip_row->[16])\n";
    >
    > if ($equip_row->[16] != undef){ # But then this test fails
    >
    > #####------######
    >
    > Now if I change the line where the conditional is to
    >
    > if ($equip_row->[16] =~ /\S+/) {
    >
    > It works. Both debug lines print like they should and the emails get sent.
    >
    > The code is working now, because I left it as a regex test, but this is
    > the craziest thing I've ever seen. I looked at it for hours, and I don't
    > think I'm missing anything stupid.
    >
    > Anyone else have any ideas ?


    Okay, I think the answer just came to me. The reason the condition

    if ($equip_row->[16] != undef)

    always fails is because you are using the != operator (instead of
    "ne") to make a comparison, which will always compare numerical
    values. As a result, $equip_row->[16] evaluates to zero (since it is
    just text with no digits), and so does undef. Then the condition is
    basically equivalent to:

    if (0 != 0)

    which, of course, will always fail. You should change that condition
    to be:

    if (defined($equip_row->[16]))

    or just:

    if ($equip_row->[16])

    since an undefined value will always evaluate to false.

    Hope this helps!

    -- Jean-Luc Romano
     
    J. Romano, Feb 21, 2004
    #1
    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. Kodeguru
    Replies:
    0
    Views:
    448
    Kodeguru
    Feb 20, 2004
  2. J. Romano
    Replies:
    0
    Views:
    500
    J. Romano
    Feb 21, 2004
  3. brewman
    Replies:
    0
    Views:
    1,468
    brewman
    Aug 28, 2003
  4. Kenneth McDonald

    Very, very strange problem with properties

    Kenneth McDonald, May 4, 2004, in forum: Python
    Replies:
    2
    Views:
    277
    Kenneth McDonald
    May 4, 2004
  5. Kodeguru

    very strange conditional problem

    Kodeguru, Feb 21, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    120
    Kodeguru
    Feb 21, 2004
Loading...

Share This Page