Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: variable set to 0 ? 0 : 1

by BrowserUk (Patriarch)
on Sep 06, 2002 at 06:44 UTC ( [id://195587]=note: print w/replies, xml ) Need Help??


in reply to Re: variable set to 0 ? 0 : 1
in thread variable set to 0 ? 0 : 1

Personally, I think I would code that as

return !!$status;

but that's probably a little too idiomatic for this OP.


Well It's better than the Abottoire, but Yorkshire!

Replies are listed 'Best First'.
Re: Re: Re: variable set to 0 ? 0 : 1
by abell (Chaplain) on Sep 06, 2002 at 08:06 UTC
    Nice golf, but your solution never returns 0, which is what the original snippet does. Furthermore, if $status is '0E0' or '0.0' it returns 1, because they both are true values, though numerically zero.
    See:
    for ( 0, 0.0, "0", "0.0", "0E0", 1, "1" ) { print $_, "\t", !!$_, "\t", ( $_==0 ? 0 : 1 ), "\n"; }

    prints out
    0               0
    0               0
    0               0
    0.0     1       0
    0E0     1       0
    1       1       1
    1       1       1
    


    Best regards

    Antonio Bellezza

    Update: As pointed out in other posts, the same behaviour of "0.0" and "0E0" is true for strings not representing numbers, which have true values, but behave as 0 in numeric comparison. The difference is a warning of type
    Argument "xyz" isn't numeric in numeric eq (==) at - line 3.
    
    when -w is enabled.

      True! Some things don't translate well from my former (C) world into Perl. I hadn't considered the possibility (or perversity) of setting a status value to a string representation of zero.

      It wasn't aimed at the OP in anycase.


      Well It's better than the Abottoire, but Yorkshire!
Re: Re: Re: variable set to 0 ? 0 : 1
by John M. Dlugosz (Monsignor) on Sep 06, 2002 at 19:01 UTC
    That's how I used to do it in C/C++ before it had a bool type, when it was necessary to canonize the value.

    If that's too idiomatic, using return asbool($status) where that's an inlined function would be readable and just as efficient.

    Note that it does a bit more than it does in a stronly-typed language. What if $status is not an int at all? The normal boolism is to treat undef and empty strings as false, also, and in one known case 0e1 is used as zero-but-true. The original test will be different from that; the !! idiom preserves it.

    —John

another variation (variable set to 0 ? 0 : 1)
by John M. Dlugosz (Monsignor) on Sep 06, 2002 at 19:07 UTC
    How about
    return $status && 1; # canonize false, leave true as-is
    for completness sake.

    update: I got that backwards. && returns the last part evaluated, and skips the right side if the left already determines the outcome. So, if $status is false it returns it unchanged; if true it evaluates the right hand side and gets a numeric 1. It canonizes true, leaves false as-is.

    $status || 0; will return $status unchanged if true, and check the rhs if false. So that canonizes false and returns true unchanged.

      return $status && 1;  # canonize false, leave true as-is

      That would be great except it doesn't canonize false. It returns $status if $status is false. So, for example, if $status is "" it will return "", not 0.

      -sauoq
      "My two cents aren't worth a dime.";
      
      $status || 0;

      I'll agree that snippet "canonizes false" but it still isn't what the original did. The original canonized numerical equality with 0. The above snippet will return "foo" because "foo" is true. The original would have returned 0 because "foo" == 0 is true.

      OK, OK, OK. I'll just be getting on with my life now. :-)

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Re: Re: variable set to 0 ? 0 : 1
by sauoq (Abbot) on Sep 07, 2002 at 01:40 UTC

    How about...

    return 0+($status!=0)

    Decent golf score and I think that works just like the original. Can anyone think of a counterexample?

    -sauoq
    "My two cents aren't worth a dime.";
    

      Apart from the fact that it gives

      Argument "fred" isn't numeric in numeric ne (!=) line 1 0

      with -w, which admittedly the original did too.

      The result of ($status!=0) will always be either 0 or 1 won't it?

      In which case, the 0+ isn't doing anything? In fact neither then are the () nor the return?

      I get the distinct feeling I'm walking into the trap here, but I cannot see it.


      Well It's better than the Abottoire, but Yorkshire!
        The result of ($status!=0) will always be either 0 or 1 won't it?

        It will either be 1 or "" (the empty string.) All of the comparison operators work this way with the obvious exceptions of <=> and cmp. So do logical negation and most of the file test operators.

        I get the distinct feeling I'm walking into the trap here, but I cannot see it.

        Well, it wasn't really meant to be a trap but, yes, you walked into it. :-)

        -sauoq
        "My two cents aren't worth a dime.";
        

Log In?
Username:
Password:

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

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

    No recent polls found