Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

by rjt (Deacon)
on Aug 03, 2013 at 11:19 UTC ( #1047697=note: print w/replies, xml ) Need Help??

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

This seems rather similar to how to refer the index of an array to another array of the same length you asked a little over an hour ago, which already has several replies.

This one is a bit different, though, so here's what I would suggest. Since you will be looping over @gl for every item in @genes, put @gl in a hash (only the keys matter). You can use a hash slice:

    my %gl; @gl{@gl} = undef;

Then, the lookup is both simple and efficient:

    say for grep { exists $gl{$_} } @genes;

Complete example:

#!/usr/bin/env perl use 5.012; use warnings FATAL => 'all'; my @genes = (qw<one two three four>); my @gl = (qw< two three five six>); my %gl; @gl{@gl} = undef; # Efficient lookups say for grep { exists $gl{$_} } @genes;


two three

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047697]
[karlgoethebier]: https://de. wiki/Bembel#/ media/File: Bembel_Zum_blauen_ Bock_1978.jpg
[choroba]: as my friend says, Ich stehe Deutsche ver nicht aber klein ;-)
[Corion]: Krug is generic pottery ware for pouring beverages, but a Bembel is painted with that blue paint and contains only (!) Apfelwein
[shmem]: Corion: alas, air too.
[Corion]: shmem: Yes, very sad!
[choroba]: doesn't sound much useful, which means I'll probably remember it
[karlgoethebier]: "that blue pain" == Salzglasur as far as i remember
[karlgoethebier]: Paint!
[shmem]: karlgoethebier the blue pain comes after all bembels are empty

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2017-05-24 08:52 GMT
Find Nodes?
    Voting Booth?