Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: using grep to match more than two words

by dewey (Pilgrim)
on Jun 12, 2007 at 06:24 UTC ( [id://620637]=note: print w/replies, xml ) Need Help??


in reply to using grep to match more than two words

This worked for me:
+ #!/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;
But after some consideration, I feel better about this slightly different phrasing:
print "\@arr1 has both" if grep {$_ eq "Sun"} @arr1 and grep {$_ eq "Moon"} @arr1;
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 :)

~dewey

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://620637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found