in reply to Re: how to access hash key from the hash value when hash key is pointing to an array of hash values?
in thread how to access hash key from the hash value when hash key is pointing to an array of hash values?
As always, ikegami provides a clear (and clearly explained), effective solution.
Here's another approach that has the advantage of a bit more concision (I doubt it will actually run any faster), but the drawback that it uses some Perl idioms that may be unfamiliar to the programmer/maintainer. You can leave out the defined test if you're absolutely sure a 'gene' will never be anything that looks false to Perl. (A similar problem, unaddressed, exists for the taxons.)
>perl -wMstrict -le "use List::Util qw(first); my %gg = ( 1567 => [ qw(NP_011 NP_012 NP_013 NP_025) ], 2000 => [ qw(NP_020 NP_021 NP_024 NP_025 NP_035) ], 9999 => [ qw(NP_900 NP_910 NP_902 0) ], 9998 => [], ); SEARCH: for my $gene_to_find (qw(NP_011 NP_025 NP_999 0)) { print qq{searching for gene '$gene_to_find':}; my $matching_taxon = first { defined first { $_ eq $gene_to_find } @{ $gg{$_} } } keys %gg ; my @all_matching_taxons = grep { defined first { $_ eq $gene_to_find } @{ $gg{$_} } } keys %gg ; print qq{ NOT found} and next SEARCH if not $matching_taxon; print qq{ first found: '$matching_taxon'}; my $all_found = join q{' '}, @all_matching_taxons; print qq{ all found: '$all_found'}; } " searching for gene 'NP_011': first found: '1567' all found: '1567' searching for gene 'NP_025': first found: '2000' all found: '2000' '1567' searching for gene 'NP_999': NOT found searching for gene '0': first found: '9999' all found: '9999'
See List::Util.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: how to access hash key from the hash value when hash key is pointing to an array of hash values?
by johngg (Canon) on Jan 29, 2010 at 23:20 UTC | |
by ikegami (Patriarch) on Jan 29, 2010 at 23:36 UTC | |
by AnomalousMonk (Archbishop) on Jan 29, 2010 at 23:38 UTC |