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

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

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

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:
    515
    Kodeguru
    Feb 20, 2004
  2. Shawn

    Very very strange. Major bug?

    Shawn, Feb 3, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    525
    Shawn
    Feb 4, 2004
  3. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    738
    Raymond Hettinger
    Jul 27, 2003
  4. Kenneth McDonald

    Very, very strange problem with properties

    Kenneth McDonald, May 4, 2004, in forum: Python
    Replies:
    2
    Views:
    399
    Kenneth McDonald
    May 4, 2004
  5. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    2,075
    Merrill & Michele
    Oct 19, 2004
  6. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    740
    jacob navia
    Oct 17, 2004
  7. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    843
    Dave Thompson
    Feb 14, 2005
  8. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,813
Loading...