in reply to using grep to match more than two words
But after some consideration, I feel better about this slightly different phrasing:+ #!/usr/bin/perl + + use strict; use warnings; my @arr1 = qw(Sun Moon Venus Pluto Neptune); my @arr2 = qw(Son Moon Venus Pluto Neptune); print "\@arr1 has both" if grep {/^Sun$/} @arr1 and grep {/^Moon$/} @arr1; print "\@arr2 has both" if grep {/^Sun$/} @arr2 and grep {/^Moon$/} @arr2;
I don't know if I should really be concerned about the regular expression's interpretation of $, but in any case since we seem to want exact equality we might as well write eq. I've opted not to use the EXPR, LIST form of grep to avoid awkward parentheses, but there might be a better way around that :)print "\@arr1 has both" if grep {$_ eq "Sun"} @arr1 and grep {$_ eq "Moon"} @arr1;
|
|---|