Why perl cannot detect zero value?

H

Hon Seng Phuah

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;
}
}
 
G

Gunnar Hjalmarsson

Hon said:
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.
 
J

Joe Smith

Hon said:
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
 
K

knobo

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
 
H

Hon Seng Phuah

Gunnar Hjalmarsson said:
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.
 
G

Gunnar Hjalmarsson

Hon said:
Refer to the code below,

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top