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.

  • Comment on Re^2: how to access hash key from the hash value when hash key is pointing to an array of hash values?
  • Select or Download Code

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

    One minor niggle, your results wording, "first found", implies some ordering in the hash which isn't there. Add some more data to the hash and you may well get a different "first found". If perhaps the taxon codes are all numeric and you said

    ... sort { $a <=> $b } keys %gg;

    then the labelling would be more meaningful.

    I hope this is of interest.

    Cheers,

    JohnGG

      One minor niggle, your results wording, "first found", implies some ordering in the hash which isn't there.

      That's not true. You are visiting the hash of arrays in some order. It might not be meaningful or static, but it's why you don't end up visiting the same node twice.

      The solution in question returns the first match encountered. It doesn't matter if the order will be different later, because you'd use it when you're expecting at most one match.

      Update: Additions to clarify

      We-elllll...

      Add some more data to the hash and you may well get a different "first found".

      Indeed, this is the case when searching for the  NP_025 gene in the example.

      I think the word 'first' has to be taken in the context of the word 'all' that is used in the example: "first taxon found containing the gene being searched for" versus "all taxons found containing the gene being searched for". Or something like that (waves hands). I would let the wording stand.