Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Answer: How to find out if X is an element in an array?

by NetWallah (Canon)
on Sep 10, 2003 at 06:29 UTC ( [id://290319]=note: print w/replies, xml ) Need Help??


in reply to Re: How to find out if X is an element in an array?
in thread How to find out if X is an element in an array?

Hmm - This method will not scale well because it creates a temporary hash. If this is a repeated search in a large array, you would be doing a lot of memory management.

Here is the equivalent code, using grep:

$X = "x"; @array = qw / x y z 1 2 3 /; return scalar grep(/^$X$/,@array) > 0;
The "scalar" isnt really necessary, but it documents the fact that grep is called in a scalar context.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-16 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found