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

I'm working on a dbi script involving starcraft II data. I want the loop that follows to execute if one player is zerg and the other player is terran. The error I'm working with is that I have a player_one_race=terran and player_two_race=protoss that is causing this loop to execute. Here is the if statement I am trying to use.

if(($player_one_race=~m/terran/ && $player_two_race=~/zerg/) || ($play +er_one_race=m/zerg/&&$player_two_race=\ ~m/terran/))
update: will use eq in the future.

Replies are listed 'Best First'.
Re: Help with a logical structure for an if statement
by brx (Pilgrim) on Apr 29, 2012 at 19:07 UTC
    Missing '~' in your code.
    It's often better to use 'and' and 'or' (lower precedence than '&&' and '||')
    update: and it's better to use 'eq' than regex when you don't really want a regex. :-)
      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}) { ... }

        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'
        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
Re: Help with a logical structure for an if statement
by toolic (Bishop) on Apr 29, 2012 at 19:10 UTC
    Here is code that compiles without errors:
    if ( ($player_one_race =~ m/terran/ && $player_two_race =~ /zerg/) || +($player_one_race =~ m/zerg/ && $player_two_race =~ m/terran/)) {}
    Consider using eq instead of regexes.
Re: Help with a logical structure for an if statement
by Kenosis (Priest) on Apr 29, 2012 at 20:22 UTC

    Here's another option:

    if("\b$player_one_race\b$player_two_race\b" =~ /(?=.*\bterran\b)(?=.*\ +bzerg\b)/) { ... }
      Your usage of backspace "\b" on the left side of the matching operator is worth a comment at least.