Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: logic problem with perl game

by PeterPeiGuo (Hermit)
on Dec 04, 2010 at 01:59 UTC ( [id://875313] : note . print w/replies, xml ) Need Help??


in reply to Re^2: logic problem with perl game
in thread logic problem with perl game

Like the following, and you need to use && not "and" (unless you get the precedence straight by using ()):

if(($response eq "arrow" && $theword eq "sparrow")) { ... } elsif(($response eq "axe" && $theword eq "wax")) { ... } elsif(($response eq "shield" && $theword eq "field")) { ... } elsif(($response eq "spear" && $theword eq "tear")) { ... } else { }

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^4: logic problem with perl game
by Anonymous Monk on Dec 04, 2010 at 03:36 UTC
    PeterPeiGuo, your doubled parentheses serve no function. Perhaps you meant to contain each condition in parentheses? But even so, your advice only complicates the code, by losing operater-precedence contrast.

    To mynameisG, regarding the original post: your original conditional code is perfect -- easy to read and does what it says. The reason "and" and "or" exist as low-precedence operators is for that very reason. You don't need to add parentheses (especially ones that do nothing) when "and" and "or" always have lower precedence than comparison operators like "eq" and ">=".