Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How to check whether an element is present in an array of array

by kilinrax (Deacon)
on Jun 05, 2009 at 10:59 UTC ( #768750=note: print w/replies, xml ) Need Help??


in reply to How to check whether an element is present in an array of array

Well, if you have something like:
my @arr1 = (1,2,3,4,5); my @arr2 = (6,7,8,9,10); my @arr3 = (11,12,13,14,15); my @mainarr = (\@arr1, \@arr2, \@arr3);
Or more simply:
my @mainarr = ([1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]);
Then you can check for the existence of an element in each subarray just by deferencing with map:
grep { $_ == $tofind } map { @$_ } @mainarray;
(note the use of '==' instead of a regex to match numbers)

However, that isn't terribly efficient, depending on the nature of the data you're actually searching. Some more context would be helpful here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2022-08-08 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?