Either scan your data or build a lookup table (see perlreftut and perldsc), as follows. Which one is better in terms of the speed/memory tradeoff depends on how big the tables are and how many lookups you are performing.
use warnings; use strict; my %barcode_hash = ( 1 => ['AGCTCGTTGTTCGATCCA','GAGAGATAGATGATAGTG','TTTT_CCCC',0], 2 => ['AGCTCGTTGTTCGATCCA','GAGAGATAGATGATAGTG','TTTT_AAAA',0], 3 => ['AGCTCGTTGTTCGATCCA','GAGAGATAGATGATAGTG','TTTT_BBBB',0], 4 => ['AGCTCGTTGTTCGATCCA','GAGAGATAGATGATAGTG','TTTT_AAAA',0], ); my $barcode_pair_35 = 'TTTT_AAAA'; for my $key (sort keys %barcode_hash) { print "Found at $key\n" if $barcode_hash{$key}[2] eq $barcode_pair_35; } # - OR - my %lookup; for my $key (sort keys %barcode_hash) { push @{ $lookup{ $barcode_hash{$key}[2] } }, $key; } print "Looked up at ".join(", ",@{$lookup{$barcode_pair_35}})."\n"; __END__ Found at 2 Found at 4 Looked up at 2, 4
Updated: Replaced the hashref $data with the hash %barcode_hash to bring the code into line with your examples.
In reply to Re: search for particular elements of hash with multiple values
by haukex
in thread search for particular elements of hash with multiple values
by pmpmmpmp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |