Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

by borisz (Canon)
on Apr 19, 2005 at 08:07 UTC ( #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 ) { ... }

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

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

    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 (Abbot) 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 prasadbabu (Prior) on Apr 19, 2005 at 08:12 UTC

    One way.


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


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

      Alberto Simões

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

        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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://449132]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-07-21 05:37 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.