Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Checking a string's presence within an array.

by broquaint (Abbot)
on May 16, 2003 at 18:47 UTC ( [id://258733]=note: print w/replies, xml ) Need Help??


in reply to Checking a string's presence within an array.

I am just curious which methods people use for this very common problem and which is the most popular
Generally I'll just use a grep and double negation e.g
my @array = qw/ a list of words /; print "yep, it's there" if !!grep { $_ eq "of" } @array; __output__ yep, it's there
Nice and efficient in newer versions of perl (5.6.1+ ?) as it won't build a list IRC. Even quicker would be a simple regex
my @array = qw/ a list of words /; print "yep, it's there" if "of" =~ /\b (?: ${\join('|', @array) }) \b/x; __output__ yep, it's there
It's not quicker on it's own (list size etc will effect performance) but if you prebuild the word list it should be quicker than grepping every time.
HTH

_________
broquaint

Log In?
Username:
Password:

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

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

    No recent polls found