Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl Monk, Perl Meditation
 
PerlMonks  

RE: better way to find list members?

by turnstep (Parson)
on Oct 16, 2000 at 20:32 UTC ( [id://37018]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to better way to find list members?

print "I am here!" if grep {/$bar/} @foo;

Update and disclaimer: Yes, all the points below are correct, this was just a quick one-liner. Don't use it for things that might actually Take Time or Need To Be Efficient. Use one of the methods above.

On second thought, why should I be that lazy? Here's a rewrite to specs:

sub IsItThere($$) { my $testarray = shift; my $string = shift; for (@$testarray) { return 1 if $_ eq $string; ## Or... return 1 if /$string/i; etc. etc. } return 0; }

Replies are listed 'Best First'.
RE: RE: better way to find list members?
by japhy (Canon) on Oct 16, 2000 at 20:36 UTC
    No no no no no no no. The FAQ strongly suggests against this, and for many reasons:
    • grep() does not short circuit -- once it finds a match, it will keep going through the rest of the list
    • the regex you use is compiled EVERY SINGLE TIME -- this can be slow and irritating
    • the regex you use will say ('foo','bar','blat') contains the element 'ar', even though it doesn't
    • you shouldn't even correct the regex (that is, add the \A and \z anchors, and \Q and \E escapes), you should use $_ eq $bar
    • DON'T USE GREP


    $_="goto+F.print+chop;\n=yhpaj";F1:eval

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://37018]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.