Why perl cannot detect zero value?

Discussion in 'Perl' started by Hon Seng Phuah, Jun 3, 2004.

  1. When I run on the below statements, I do not understand why it zero
    and here. The $a contains value zero. Why can't perl skip the print
    statement?

    #!/usr/intel/bin/perl
    $count = "17.0";
    if ($count =~ /\./)
    {
    ($before_decimal, $a) = split(/\./, $count);
    if (!$before_decimal || $before_decimal =~ /\D/)
    {
    exit;
    }

    if (!$a)
    {
    print "$a";
    }
    if (!$aft_decimal || (!$aft_decimal =~ /0$/ && $aft_decimal =~
    /\D/))
    {
    print "Here";
    exit;
    }
    }
    Hon Seng Phuah, Jun 3, 2004
    #1
    1. Advertising

  2. Hon Seng Phuah wrote:
    > When I run on the below statements, I do not understand why it zero
    > and here.


    Do you mean why it prints "0Here"? That's because you coded it to do so.

    Maybe you should try to explain in English what it is you are *trying*
    to do. I'm not able to figure it out from the code.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 3, 2004
    #2
    1. Advertising

  3. Hon Seng Phuah

    Joe Smith Guest

    Hon Seng Phuah wrote:

    > if (!$b || $b =~ /\D/)


    That's the same as
    if (
    (not defined $b) || ($b eq "") || ($b eq "0") || ($b == 0) || ($b =~ /\D/)
    )

    > if (!$a)
    > {
    > print "$a";
    > }


    That's the same as
    print "$a" if (not defined $a or $a eq "" or $a eq "0" or $a == 0);
    so it will print "0" with the example given.

    > if (!$aft_decimal || (!$aft_decimal =~ /0$/ && $aft_decimal =~
    > /\D/)) { print "Here"; }


    Since $aft_decimal is undef, !$aft_decimal is true, therefore the
    print statement will be executed.

    Changing that to
    if (!$a || (anything at all here)) {print "Here"}
    will print "Here" since !$a is true when $a == "0".

    So, perl is doing exactly what you've told it to do when it prints "0Here".
    It is detecting zero in both cases and printing both strings.
    -Joe
    Joe Smith, Jun 3, 2004
    #3
  4. Hon Seng Phuah

    knobo Guest

    (Hon Seng Phuah) wrote in message news:<>...
    > When I run on the below statements, I do not understand why it zero
    > and here. The $a contains value zero. Why can't perl skip the print
    > statement?


    if (0) => never
    if (!0) => allways

    > #!/usr/intel/bin/perl
    > $count = "17.0";
    > if ($count =~ /\./)
    > {
    > ($before_decimal, $a) = split(/\./, $count);


    $a == 0

    > if (!$before_decimal || $before_decimal =~ /\D/)
    > {
    > exit;
    > }
    >
    > if (!$a)


    Remember if (!0) => allways

    > {
    > print "$a";
    > }
    > if (!$aft_decimal || (!$aft_decimal =~ /0$/ && $aft_decimal =~
    > /\D/))
    > {
    > print "Here";
    > exit;
    > }
    > }
    knobo, Jun 3, 2004
    #4
  5. Gunnar Hjalmarsson <> wrote in message news:<3pCvc.1092$>...
    > Hon Seng Phuah wrote:
    > > When I run on the below statements, I do not understand why it zero
    > > and here.

    >
    > Do you mean why it prints "0Here"? That's because you coded it to do so.
    >
    > Maybe you should try to explain in English what it is you are *trying*
    > to do. I'm not able to figure it out from the code.


    Oops. I missed some words on my sentense.

    Refer to the code below,

    #!/usr/intel/bin/perl
    $count = "17.0";
    if ($count =~ /\./)
    {
    ($before_decimal, $a) = split(/\./, $count);
    if (!$before_decimal || $before_decimal =~ /\D/)
    {
    exit;
    }

    if (!$a)
    {
    print "$a";
    }
    }

    I do not understand why the above code print 0. The $a contains value,
    "0". I thought perl should skip the print statement since the $a has
    been initialized.
    Hon Seng Phuah, Jun 3, 2004
    #5
  6. Hon Seng Phuah wrote:
    > Gunnar Hjalmarsson wrote:
    >> Maybe you should try to explain in English what it is you are
    >> *trying* to do.

    >
    > Refer to the code below,


    <snip>

    > if (!$a)
    > {
    > print "$a";
    > }
    > }
    >
    > I do not understand why the above code print 0. The $a contains
    > value, "0". I thought perl should skip the print statement since
    > the $a has been initialized.


    Okay. As others have explained, both 0 (the number) and '0' (the
    string) return a false value, just like the nullstring or an undefined
    value. Accordingly, the statements

    print "False\n" unless 0;
    print "False\n" unless '0';
    print "False\n" unless '';
    print "False\n" unless undef;

    all print "False\n".

    Since $a in your example contains '0', $a is false, and the opposite
    !$a is true.

    Maybe this is what you want to do:

    unless (!$a or $a eq '0')
    {
    print "$a";
    }

    HTH

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 3, 2004
    #6
    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. Zhiqiang Ye
    Replies:
    53
    Views:
    10,240
    Dan Pop
    Jun 28, 2004
  2. Gerard Flanagan
    Replies:
    3
    Views:
    443
    Terry Hancock
    Nov 19, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    875
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  5. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    670
    Walter
    Jul 1, 2004
Loading...

Share This Page