Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Help with a logical structure for an if statement

by JavaFan (Canon)
on Apr 29, 2012 at 19:32 UTC ( #967969=note: print w/ replies, xml ) Need Help??


in reply to Re: Help with a logical structure for an if statement
in thread Help with a logical structure for an if statement

It's often better to use 'and' and 'or' (lower precedence than '&&' and '||')
I don't get this. Lower precedence operators are better to use than higher precedence ones, even if they have existed in Perl longer, and are used in a myriad of other languages? If so, do you have some arguments to back up this claim?

Perhaps you meant to write "I would use" where you wrote "It's often better to use"?

I would write it as:

if ($player_one_race eq 'terran' && $player_two_race eq 'zerg' || $player_one_race eq 'zerg' && $player_two_race eq 'terran') { ... }
That is, I'd lose the redundant parenthesis, use the standard operators, and use whitespace to make the symmetry clear. Or:
my %races; $races{$_} = 1 for $player_one_race, $player_two_race; if ($races{zerg} && $races{terran}) { ... }


Comment on Re^2: Help with a logical structure for an if statement
Select or Download Code
Re^3: Help with a logical structure for an if statement
by tobyink (Abbot) on Apr 30, 2012 at 07:43 UTC

    Or even:

    if ([sort $player_one_race, $player_two_race] ~~ [qw(terran zerg)]) { # TIMTOWTDI }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: Help with a logical structure for an if statement
by brx (Pilgrim) on Apr 30, 2012 at 09:12 UTC
    Oops. I'm not sure to understand what you said but here some explanation with my well-known "beach-English".
    Because and and or have the lower precedence ( lower than = for example), it is for me simpler : no need to use extra parenthesis when they are used as 'if...then' way.
    With && and || I always have to check 'perldoc perlop' to find precedence. So, OK, it's only my choice.
    Some not tested examples I would write
    And: $var=$den and $m/=$var
    &&: $var=$one&&$two

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-09-18 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls