Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: The True but Zero value?

by Marshall (Canon)
on Mar 20, 2016 at 22:48 UTC ( [id://1158389]=note: print w/replies, xml ) Need Help??


in reply to Re: The True but Zero value?
in thread The True but Zero value?

Yes, there is "0 but true" and that is very, very seldom known thing. You are correct about that extra detail that I'd forgotten, but I have seen it before.

Thanks for adding that point!

My main point got lost in that there is such a thing as "True but Zero value". I just blew it in my post. With my current small screen, number "0" and letter "O" look almost the same. I blew it. I admit it. Let's move on.

Update: Here is some code that proves your comment:

#!/usr/bin/perl use warnings; use strict; my $x = "0 but true"; my $y = "0 but whatever"; $x += 0; $y += 0; __END__ Prints: Argument "0 but whatever" isn't numeric in addition (+) at C:\Projects_Perl\0buttrue.pl line 9. Process completed successfully
You are correct, my $x = "0 but true"; is "special". 0E0 is more often seen, but this works the same way.

Replies are listed 'Best First'.
Re^3: The True but Zero value?
by Athanasius (Archbishop) on Mar 21, 2016 at 03:35 UTC

    Hello Marshall,

    0E0 is more often seen, but this works the same way.

    Yes, but only when it appears as a string; when it appears as a number, it is zero and false:

    #! perl use strict; use warnings; for ( 0, 0E0, '0E0', 0.0, '0.0', 0.00, '0.00', 00, '00', '0', '0 but true', '0 BUT TRUE', '0 but true ', '1 and true', ) { printf "%-11s %d %s\n", $_, ($_ + 0), ($_ ? 'true' : 'false'); }

    Output:

    13:25 >perl 1575_SoPW.pl 0 0 false 0 0 false 0E0 0 true 0 0 false 0.0 0 true 0 0 false 0.00 0 true 0 0 false 00 0 true 0 0 false 0 but true 0 true Argument "0 BUT TRUE" isn't numeric in addition (+) at 1575_SoPW.pl li +ne 34. 0 BUT TRUE 0 true Argument "0 but true " isn't numeric in addition (+) at 1575_SoPW.pl l +ine 34. 0 but true 0 true Argument "1 and true" isn't numeric in addition (+) at 1575_SoPW.pl li +ne 34. 1 and true 1 true 13:25 >

    The string '0' is documented as special: it is zero and false. Other strings are true1 regardless of their numerical values. It appears that the isn't numeric warning applies only when the numerical conversion is not “pure” (because the string contains non-numeric characters).

    It seems strange that this behaviour is so poorly documented. The '0 but true' special case is not mentioned in perlsyn#Truth-and-Falsehood. My experiments suggest that this special string must be character-for-character exact for the special behaviour to apply. For example, '0 but true ' (note the extra space at the end) still generates an isn't numeric warning.

    Update: 1Except the empty string, '', which is also documented to be false.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks, I didn't know that "0" was documented to be "false" in a string context. Every other string should be "true".

      That's just as weird as the "0 but true" situation. I don't know where that is documented and I've only seen it VERY, VERY rarely. Yes, it has to be spelled exactly like that and in the proper case.

      I'm building a new main computer and started using a wimpy laptop to investigate some other problem and installed 5.22 (12 levels since 5.10) and I thought something had broken, but the only thing that "broke" were my eyeballs in conjunction with the font being used.

      "0E0" is a documented part of the DBI, although lots of folks aren't aware of that. When you do an SQL UPDATE,SELECT or whatever, it is returned to say that the statement "worked", but it didn't do anything (0 rows affected). Its been a couple of years since I mucked with the DBI, but I've seen code that counts upon "0E0" quite a few times. When I do it, I put in a comment to explain what's going on, otherwise this fine point is obscure.

      There are a lot of fine points and nuances with Perl. I will never know them all.

        BTW, there is nothing special about 0E0. It could be 0E5 for instance. The important point is that the value is in engineering notation so is true as a string, but not true when interpreted as a number. You could also use "00" for this purpose as well.

        "0 but true" is special however in that perl has specific exemptions for it internally.

        ---
        $world=~s/war/peace/g

        Not only DBI. The E0 is documented in perlop at the flip flop:
        #!/usr/bin/perl use warnings; use strict; while (<DATA>) { if (my $flipflop = /start/ .. /end/) { print "$flipflop: $_"; } } __DATA__ before start 1 2 3 end after

        Output:

        1: start 2: 1 3: 2 4: 3 5E0: end
        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1158389]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found