Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: The True but Zero value?

by Marshall (Canon)
on Mar 21, 2016 at 04:49 UTC ( [id://1158398]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: The True but Zero value?
by demerphq (Chancellor) on Mar 21, 2016 at 13:39 UTC

    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

      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".

        I should have mentioned in my original post that there is another set of special cases for this purpose. Perl silently ignores leading and trailing whitespace when numifying a string (which AFAIK is not widely known nor documented, although a pointer to the relevant docs are welcome). So there are an infinite set of strings which are false numerically, but true in boolean context. (And an infinite set of strings which are numerically equivalent to any integer you choose.)

        perl -wle'my $n= (" " x 1_000_000) . "0" . (" " x 1_000_000); print $n + ? "true" : "false", " as a bool, prenumification"; print 0+$n ? "tru +e" : "false", " as a bool, numified"; print $n ? "true" : "false", " +as a bool, postnumification";' true as a bool, prenumification false as a bool, numified true as a bool, postnumification

        This is not generally a problem except for people who write serializers and need to ensure that " 0 " round trips correctly.

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

Re^5: The True but Zero value?
by choroba (Cardinal) on Mar 22, 2016 at 17:15 UTC
    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,
      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found