Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: The True but Zero value?

by Athanasius (Chancellor)
on Mar 21, 2016 at 03:35 UTC ( #1158394=note: print w/replies, xml ) Need Help??

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

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'); }


13:25 >perl 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 li +ne 34. 0 BUT TRUE 0 true Argument "0 but true " isn't numeric in addition (+) at l +ine 34. 0 but true 0 true Argument "1 and true" isn't numeric in addition (+) at 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,

Replies are listed 'Best First'.
Re^4: The True but Zero value?
by Marshall (Abbot) on Mar 21, 2016 at 04:49 UTC
    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.


        demerphq is quite correct. The only thing that is "special" about "0E0" is that it is documented in the Perl DBI. I have classic EE training so working with exponents is easy for me. However, I like the 0E0 idea and find it preferable to 0E5 or say "00" because a simple typo "0" gets us into another special case which might be hard to find in the code. This 0E0 values allows the DBI to express 2 ideas in one return value: 1) did it work or not? and 2)how many results were obtained.

        I think we've "nailed this one". Here is summary of these special things:

        1. string "0", this would normally be "true" in a string context, because all non-null strings are "true". However this is a special case where it evaluates to "false" in a string context. So the special case code allows a more intuitive behavior although technically an exception to the standard rules. Of course this is numeric zero in a numeric context as would be expected.

        2. string "0 but true", this is a special case where you can do math on this, e.g.. $val +=0; and no warning will occur even with warnings enabled. Normally there would be a warning of $val is non-numeric at line x...". This of course is "true" in a string context (all non-null are normally "true") and zero in a numeric context, just like it says. This is not seen as often as the next string ("0E0") which accomplishes the same thing.

        3. string "0E0", This could have been any string that evaluates to zero in a numeric context, e.g. "00" or "0E5", However 0E0 is the documented return value in the DB for the "0 but true" value. Since this is a valid number in a string context, no special code need execute like in case (2).

        Note: When I work with something that can return (2) or (3) and I make use of that, I add a comment to explain what is going on. Even if you know what is going on when you write the code, 3 years later, that might be "fuzzy".
      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


      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,
        Yes, this is quite correct although here the string "5E0" is meaning something different that "zero but true". Here it means "hey, this is a special kind of Five!". But we get the same idea of expressing 2 things with a single return value. Here the idea expressed is that this is the "END" and the END value is 5.

        E0 means 10**0 which is "1" numerically.
        So, 5E0 is a way of saying 5*1.
        The E0 means that this is a "special Five" and indeed it is with the flip-flop operator.

        0E0 is way of saying "0*1". And that this is a special zero.
        demerphq pointed out that 0E5 is also zero, the exponent doesn't matter. This means 0 * 10**5. However I see a certain simplicity to E0 (10**0)== 1.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1158394]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-01-20 23:07 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.