very strange conditional problem

Discussion in 'Perl Misc' started by Kodeguru, Feb 21, 2004.

  1. Kodeguru

    Kodeguru Guest

    ** I also posted this in comp.lang.perl **


    I just finished debugging a very strange problem, and was wondering if
    any of you had ever seen it before. My code is very long, so I will only
    post the interesting parts.

    ok, so using DBD::Sybase and some other stuff, we do a select query on a
    database and store the return like this:

    #####------######

    $sql = "SELECT blah blah blah";

    $sth = $dbh->prepare();

    $sth->execute();

    while (my $equip_row = $sth->fetchrow_arrayref()){

    # ....

    # 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

    # and this line doesn't print at all
    print "$equip_row->[16] is not undef, so...\n";

    # sends email
    # ....

    }
    }


    #####------######

    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 ?

    --
    Michael O'Malley Jr.
    President
    Pillar Open Source Technologies, LLC.
    Kodeguru, Feb 21, 2004
    #1
    1. Advertising

  2. Kodeguru

    ko Guest

    Kodeguru wrote:
    >
    > ** I also posted this in comp.lang.perl **
    >
    > I just finished debugging a very strange problem, and was wondering if
    > any of you had ever seen it before. My code is very long, so I will only
    > post the interesting parts.
    >
    > ok, so using DBD::Sybase and some other stuff, we do a select query on a
    > database and store the return like this:
    >
    > #####------######
    >
    > $sql = "SELECT blah blah blah";
    >
    > $sth = $dbh->prepare();
    >
    > $sth->execute();
    >
    > while (my $equip_row = $sth->fetchrow_arrayref()){
    >
    > # ....
    >
    > # 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

    ------------^^^^^^^^^^^^^^^^^^^^^^^^^^

    The != operator expects *numeric* operands/arguments. Both
    $equip_row->[16] and undef are converted to numbers. So basically what
    happens is:

    if ( 0 != 0 ) {

    > # and this line doesn't print at all
    > print "$equip_row->[16] is not undef, so...\n";


    That's why this block never gets executed.

    > # sends email
    > # ....
    >
    > }
    > }
    >
    >
    > #####------######


    [snip]

    HTH -keith
    ko, Feb 21, 2004
    #2
    1. Advertising

  3. Kodeguru

    Kodeguru Guest

    ko wrote:
    >
    > The != operator expects *numeric* operands/arguments. Both
    > $equip_row->[16] and undef are converted to numbers. So basically what
    > happens is:
    >
    > if ( 0 != 0 ) {
    >
    >> # and this line doesn't print at all
    >> print "$equip_row->[16] is not undef, so...\n";

    >
    >
    > That's why this block never gets executed.
    >
    >> # sends email
    >> # ....
    >>
    >> }
    >> }
    >>
    >>
    >> #####------######

    >
    >
    > [snip]
    >
    > HTH -keith


    I tried it with ne as well, as in

    if ($equip_row->[16] ne undef){

    and that didn't work either.

    --
    Michael O'Malley Jr.
    President
    Pillar Open Source Technologies, LLC.
    Kodeguru, Feb 21, 2004
    #3
  4. Kodeguru

    Kodeguru Guest

    Kodeguru wrote:


    > I tried it with ne as well, as in
    >
    > if ($equip_row->[16] ne undef){
    >
    > and that didn't work either.
    >



    Hmm,
    no, perhaps I'm mistaken. Maybe I didn't try it with 'ne'.

    Thanks.

    --
    Michael O'Malley Jr.
    President
    Pillar Open Source Technologies, LLC.
    Kodeguru, Feb 21, 2004
    #4
  5. Kodeguru

    ko Guest

    Kodeguru wrote:
    > Kodeguru wrote:
    >
    >
    >> I tried it with ne as well, as in
    >>
    >> if ($equip_row->[16] ne undef){
    >>
    >> and that didn't work either.
    >>

    >
    >
    > Hmm,
    > no, perhaps I'm mistaken. Maybe I didn't try it with 'ne'.
    >
    > Thanks.
    >


    You probably don't want to use this type of construct anyway, since it
    triggers a warning - you do have warnings enabled right? :)

    Its more common to use something like:

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

    keith
    ko, Feb 21, 2004
    #5
  6. ko <> wrote:
    > Kodeguru wrote:



    >> if ($equip_row->[16] != undef){ # But then this test fails

    > ------------^^^^^^^^^^^^^^^^^^^^^^^^^^
    >
    > The != operator expects *numeric* operands/arguments. Both
    > $equip_row->[16] and undef are converted to numbers. So basically what
    > happens is:
    >
    > if ( 0 != 0 ) {



    > That's why this block never gets executed.



    You should always enable warnings when developing Perl code!

    Kodeguru has wasted a bunch of man-minutes by asking thousands
    of people around the world to troubleshoot a problem that a
    machine could have spotted in a small part of a single second!

    Please do not ask people to do the work of a machine,
    they won't like it.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 21, 2004
    #6
  7. Kodeguru

    J. Romano Guest

    Kodeguru <> wrote in message news:<2SGZb.27067$>...
    >
    > # 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
    >
    > # and this line doesn't print at all
    > print "$equip_row->[16] is not undef, so...\n";
    >
    > #####------######
    >
    > 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.
    >
    > Anyone else have any ideas ?


    I have an idea, even though I do not know the exact reason why your
    code is not working as you'd expect.

    My thought is that your Perl interpreter doesn't like comparing a
    scalar to undef. Therefore, I suggest that, instead of comparing a
    scalar to see if it doesn't equal undef, try seeing if it is defined
    by using the "defined" keyword. Try changing the condition:

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

    to this one:

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

    See if that works.

    Happy Perling!

    -- Jean-Luc
    J. Romano, Feb 21, 2004
    #7
  8. Kodeguru

    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
    #8
  9. Kodeguru <> wrote in message news:<2SGZb.27067$>...
    > ** I also posted this in comp.lang.perl **



    > if ($equip_row->[16] != undef){ # But then this test fails


    Should be: if (defined $equip_row->[16])

    HTH,
    Chris
    Chris Charley, Feb 21, 2004
    #9
  10. Kodeguru

    Kodeguru Guest

    Thank you all,


    I did have warnings turned on, but it just didn't seem to help. The
    warning I got didn't seem to explain the problem, and it is just a
    "warning". I'm sorry to have wasted all of your man minutes.
    Kodeguru, Feb 21, 2004
    #10
    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:
    429
    Kodeguru
    Feb 20, 2004
  2. J. Romano
    Replies:
    0
    Views:
    483
    J. Romano
    Feb 21, 2004
  3. J. Romano
    Replies:
    0
    Views:
    451
    J. Romano
    Feb 21, 2004
  4. brewman
    Replies:
    0
    Views:
    1,434
    brewman
    Aug 28, 2003
  5. Kenneth McDonald

    Very, very strange problem with properties

    Kenneth McDonald, May 4, 2004, in forum: Python
    Replies:
    2
    Views:
    255
    Kenneth McDonald
    May 4, 2004
Loading...

Share This Page