my @alternatives = qw(ABA SCO ACC PHC GHF); my $target = 'ABA'; if (grep {$target eq $_} @alternatives) { print "$target is in (@alternatives)\n"; } else { print "$target is not in (@alternatives)\n"; }
The meat is this: if (grep {$target eq $_} @alternatives) ....
Here's how it works. First, grep will iterate over the elements in @alternatives, and compare each element to your $target value. In scalar context (provided by the if(CONDITION) construct), grep returns the number of matches. In Perl, any numeric value that is not zero is true in a Boolean sense. So getting a match on one of the elements will make the condition true.
You can set up an "all" relationship (instead of "any") like this:
if (@alternatives == grep {$target eq $_} @alternatives) {...Here we're asserting that every element in @alternatives must match $target.
Dave
In reply to Re: Sparing multiple 'or's
by davido
in thread Sparing multiple 'or's
by Denis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |