Prev: FAQ 6.12 Can I use Perl regular expressions to match balanced text?
Next: FAQ 7.9 How do I create a module?
From: Faith Greenwood on 18 Jan 2010 19:47 I have the following: use strict; use warnings; my $number=0; print "This:$number\n"; print "That:$number\n" if $number; __END__ Of the two lines that print the variable, why does it print the first line but not the second? If I change the $number to equal 1, then both lines print fine. thank you
From: Kevin Sproule on 18 Jan 2010 20:09 Faith, In Perl the number 0 is false and any number other than zero is true. The line with "if $number" will only execute when $number is not equal to 0. Kevin Sproule "Faith Greenwood" <fgnowfg(a)gmail.com> wrote in message news:07de4f8d-63bf-438d-a4fd-d41fcd4a5da0(a)r5g2000yqb.googlegroups.com... >I have the following: > > use strict; > use warnings; > > my $number=0; > print "This:$number\n"; > print "That:$number\n" if $number; > > __END__ > > Of the two lines that print the variable, why does it print the first > line but not the second? > If I change the $number to equal 1, then both lines print fine. > > thank you
From: sreservoir on 18 Jan 2010 20:38 On 1/18/2010 7:47 PM, Faith Greenwood wrote: > I have the following: > > use strict; > use warnings; > > my $number=0; > print "This:$number\n"; > print "That:$number\n" if $number; > > __END__ > > Of the two lines that print the variable, why does it print the first > line but not the second? > If I change the $number to equal 1, then both lines print fine. > > thank you because 0 is, traditionally, not true, even in perl where false is canonically undef. -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: Uri Guttman on 18 Jan 2010 21:13 >>>>> "s" == sreservoir <sreservoir(a)gmail.com> writes: s> On 1/18/2010 7:47 PM, Faith Greenwood wrote: >> I have the following: >> >> use strict; >> use warnings; >> >> my $number=0; >> print "This:$number\n"; >> print "That:$number\n" if $number; >> >> __END__ >> >> Of the two lines that print the variable, why does it print the first >> line but not the second? >> If I change the $number to equal 1, then both lines print fine. >> >> thank you s> because 0 is, traditionally, not true, even in perl where false is s> canonically undef. undef, '', 0 and '0' are the boolean false values for perl. there is no canonical one. you can use them how you wish but they will all be false under perl's boolean tests (which includes if modifier). for the OP, what did you think 'if' means? that it was defined? for that use the defined function: print "That:$number\n" if defined $number; that will print. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Ben Morrow on 18 Jan 2010 21:05 Quoth sreservoir <sreservoir(a)gmail.com>: > On 1/18/2010 7:47 PM, Faith Greenwood wrote: > > I have the following: > > > > use strict; > > use warnings; > > > > my $number=0; > > print "This:$number\n"; > > print "That:$number\n" if $number; > > > > __END__ > > > > Of the two lines that print the variable, why does it print the first > > line but not the second? > > If I change the $number to equal 1, then both lines print fine. > > > > thank you > > because 0 is, traditionally, not true, even in perl where false is > canonically undef. No. False in perl is canonically a dualvar that is the empty string in string context and 0 in numeric context (so there are no warnings on numeric conversion, unlike a plain ""). ~% perl -E'say defined !1' 1 ~% perl -wE'say 0 + !1' 0 ~% perl -wE'say "[", ("" . !1), "]"' [] ~% perl -wE'say 0 + ""' Argument "" isn't numeric in addition (+) at -e line 1. 0 ~% True is canonically the string "1". Ben
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: FAQ 6.12 Can I use Perl regular expressions to match balanced text? Next: FAQ 7.9 How do I create a module? |