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

I am trying to write a script using the or operator and nothing seems to be working. For example: if ($color eq "red|blue|green") or is it: if ($color eq "red" || "blue" || "green") Am i way off here?

Replies are listed 'Best First'.
•Re: OR operator
by merlyn (Sage) on Apr 10, 2003 at 00:55 UTC
Re: OR operator
by DrManhattan (Chaplain) on Apr 10, 2003 at 00:54 UTC
    Here's a correct syntax:
    if ($color eq "red" || $color eq "blue" || $color eq "green") { # something }

    -Matt

Re: OR operator
by DrManhattan (Chaplain) on Apr 10, 2003 at 01:15 UTC
    And just for grins:
    if (grep { $color eq $_ } qw(red green blue)) { # something }

    -Matt

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: OR operator
by Coruscate (Sexton) on Apr 10, 2003 at 01:04 UTC

    Also, you could use a regex to do this. As well, you could add the 'i' modifier to the regex if you wish to allow case-insensitivity, to allow such things as 'Red', 'GrEeN', 'BLUe':

    if ($color =~ /\A(?:red|blue|green)\z/) { # do something here }


    Update: Fixed code to use \A and \z as mentioned below by Juerd and merlyn. I still haven't shaken the habit of using the nasty ^ and $ anchors. I also don't like the use of the /s modifier much, so I had best get used to the better-used anchors.


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: OR operator
by tachyon (Chancellor) on Apr 10, 2003 at 01:08 UTC

    In addition to the above you can use a RE perlre and do:

    if ( $color =~ m/^(red|green|blue)$/ ) { blah }; # a common perl idiom is: my @colors = qw( red green blue pink fuscia ); my $color_re = join '|', @colors; $color_re = qr/$color_re/; # compile RE for efficiency if ( $color =~ m/^(?:$color_re)$/ ) { blah }

    Note the ?: stops capturing the color matched in the RE in $1

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        So use \z if it make you happy - it all depends upon your context. I didn't bother cause I knew someone to rise to the bait {grin}. I can be an important point (usually it is not) so it deserves reiteration.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: OR operator (hash)
by tye (Sage) on Apr 10, 2003 at 16:55 UTC

    So many responses... But you are all WRONG!

    my %primary; BEGIN { @primary{qw( red blue green )}= (1)x3; } ... if( $primary{$color} ) { ....
    ;)

                    - tye
      BEGIN { @primary{qw( red blue green )} = (); } if(exists $primary{$color})
      :P

      Makeshifts last the longest.

Re: OR operator
by kelan (Deacon) on Apr 10, 2003 at 14:48 UTC

    In Perl6, you'll be able to use something very close to this syntax, though.

    ### <Perl6> if ($color eq ("red" | "blue" | "green")) {} if ($color eq any("red", "blue", "green")) {} ### </Perl6>

    kelan


    Perl6 Grammar Student