Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to I check if a value is present in an array?

by moritz (Cardinal)
on Aug 03, 2013 at 10:54 UTC ( #1047694=note: print w/ replies, xml ) Need Help??


in reply to How to I check if a value is present in an array?

grep doesn't do what you think. It returns a list of all elements where the search expression is true. The search expression in your case is $genes[$index], so if that's a true value, grep returns all the elements of @gl.

What you probably want to write instead is something like

for my $current (@genes) { if (grep { $_ eq $current } @gl) { print $current; } }

Though of course if either (or both) of these arrays is large, that is rather inefficient, and you might consider storing stuff in a hash in the first place.


Comment on Re: How to I check if a value is present in an array?
Select or Download Code
Re^2: How to I check if a value is present in an array?
by Tux (Monsignor) on Aug 03, 2013 at 12:17 UTC

    If @gl is (or can be) large/huge, List::Util's first can be an important speedup.

    use List::Util qw( first ); for my $current (@genes) { say $current if first { $_ eq $current } @gl; }

    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls