Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Better expression for $x = !!$y||0

by borisz (Canon)
on Apr 19, 2005 at 08:07 UTC ( [id://449132]=perlquestion: print w/replies, xml ) Need Help??

borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I search for a better/shorter/more_readable way to set a var $x to exactly 0 or 1 depending on another var.
For example:
if ( $y ) { $x = 1; ... } else { $x = 0; }
I rewrote this to:
if ( $x = !!$y || 0 ) { ... }
Boris

Replies are listed 'Best First'.
Re: Better expression for $x = !!$y||0
by pelagic (Priest) on Apr 19, 2005 at 08:14 UTC
    $x = $y ? 1 : 0;
    and: timtowtdi ...

    pelagic
Re: Better expression for $x = !!$y||0
by reasonablekeith (Deacon) on Apr 19, 2005 at 08:53 UTC
    quote:

    I rewrote this to:

    if ( $x = !!$y || 0 ) { ... }
    ...

    I'm sorry, but that's just awful. I'd never guess what you were actually trying to do here if I stumbled across this code, regardless of how correct it might be.

    In my opinion there's not much wrong with your first example, and although the conditional operator does add some brevity, the if else construct is almost certainly more readable to more people.

Re: Better expression for $x = !!$y||0
by Anonymous Monk on Apr 19, 2005 at 09:26 UTC
    I'd go for $y ? 1 : 0. Other suggestions, like !!$y || 0 or 1 - !$y may not return 0 or 1 if $y has overload magic.

    It also doesn't require the reader to know that negating a false value returns 1 (and not some other true value).

Re: Better expression for $x = !!$y||0
by ysth (Canon) on Apr 19, 2005 at 09:00 UTC
    Shorter: $x = 1-!$y

    Better, IMO, but much depends on what the reader knows about x and y from the surrounding code:

    $x = 0; if ($y) { $x = 1; ... }
        I said shorter, I didn't say accurate :). But really, if you presume an overloaded ! that returns something other than perl's true and false values (and that's presuming an awful lot) there's nothing you can do to $y besides examine it in boolean context with if or ?:.
Re: Better expression for $x = !!$y||0
by salva (Canon) on Apr 19, 2005 at 13:11 UTC
    for maximum readibility and if you are doing this kind of conversion in more than one place, you can also create a new "booleanize" operator:
    sub booleanize ($) { shift ? 1 : 0 } $x = booleanize $y;
Re: Better expression for $x = !!$y||0
by ghenry (Vicar) on Apr 19, 2005 at 08:18 UTC

    See the Conditional Operator, to understand the above better.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      Thanks all, sure I know about the conditional. It just look unnatural in a if statement to me. Thats why I used || in the first place. But since all answers are more or less the same. I shouldn't be so picky.
      Boris

        Since you don't like seeing the conditional operator in the if, why don't you move it to the outside?

        $x = $y ? 1 : 0; if ($x) { ... }

        It has the added advantage of removing an assignement from an if's condition, something I find quite unnatural and unreadable.

Re: Better expression for $x = !!$y||0
by prasadbabu (Prior) on Apr 19, 2005 at 08:12 UTC

    One way.

    $y?$x=0:$y=1;

    update:$x=$y?0:1 is correct as ambs mentioned.

    Prasad

      Not properly this (typo?). The other reply is correct ($x=$y?0:1)

      Alberto Simões

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-25 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found