Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Smartmatch alternatives

by Eily (Curate)
on Dec 17, 2013 at 13:18 UTC ( #1067467=note: print w/ replies, xml ) Need Help??


in reply to Smartmatch alternatives

TIMTOWTDI.

I often use the grep solution $contained = grep { $_ eq $searched } @array; or even grep /^\Q$searched$/,  @array

If you are going to search for several values in the same @array, a hash may be a good idea (and unless order is important, or the same value can appear more than once, you could just use a hash all the way):

my %hash = map { $_ => 1 } @array; for $search (@listOfScalars) { say '@array contains '.$_ if exists $hash{$_}; }

Or there's also the first_index function from List::AllUtils. Which is similar to the grep solution, except it stops at the first match.


Comment on Re: Smartmatch alternatives
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-07-04 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls