AWallBuilder has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, I am getting confused traversing 'multi-level' hashes, and cannot seem to find the exact answer I am looking for on similar posts.
First question, are these two notations to assign values to the 'deeper keys' equivalent? If the first commented one even correct?
#? $HoFlg1=(Query_id=>{$Query_id},Subj_id=>{$Subj_id},bit_score=> +{$bit_score},Flag=>$Flag1); #? $HoFlg2=(Query_id=>{$Query_id},Subj_id=>{$Subj_id},bit_score=> +{$bit_score},Flag=>$Flag2); $HoFlg1->{$Query_id}->{$Subj_id}->{$bit_score}=$Flag1; $HoFlg2->{$Query_id}->{$Subj_id}->{$bit_score}=$Flag2;
Second question, how do I find the maximum key value for the 3rd level key (ie. for all second level keys for the same key1?). That is for each $Query_id, I want to find the maximum $bit_score (for all $Subj_ids)
here are some incorrect/partial attempts
thanks in advance
##### For each Query_id find top bit score sub largest_key { my $hash = @_; my ($large_key) = each %$hash; foreach (my ($key) = keys %$hash) { if ($key > $large_key) { $large_key = $key; } } return $large_key; } ##### For each Query_id find top bit score my %Hotop_bit; foreach my $key1 (keys %{$HoFlg2}){ foreach my $key2 (keys %{$HoFlg2->{$key1}}){ foreach my $key3 (keys %{$HoFlg2->{$key1}->{$key2}}) { $top_bit=&largest_key(%{$HoFlg2->{$key1}->{$ke +y2}}) print "key1\t$key1\tkey2$key2\tbitscore\t$key3 +\n"; $Hotop_bit{$key1}=$top_bit; #??probably wrong } } print "for query\t$key1 top bit score is\t$Hotop_bit{$key1}\n" +; } ###For each Query_id find top bit score - using => hash creation ; foreach my $key1 (keys %{$HoFlg2}){ my @bits=map{$HoFlg2{$key1}->{bit_score}} keys(%HoFlg2); $top_bit=reverse sort(@bits)[0]; $Hotop_bit{$key1}=$top_bit; } ###For each Query_id find top bit score - using => hash creation ; foreach my $key1 (keys %{$HoFlg2}) my $highest_bit=( reverse sort { $HoFlg2{$a}->{bit_score} <=> +$HoFlg2{$b}->{bit_score}} keys(%HoFlg2) )[0];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort hash ignoring one hash level
by NetWallah (Canon) on May 14, 2012 at 15:40 UTC | |
by AWallBuilder (Beadle) on May 14, 2012 at 21:03 UTC | |
by Cristoforo (Curate) on May 15, 2012 at 00:40 UTC | |
by NetWallah (Canon) on May 15, 2012 at 02:36 UTC | |
|
Re: sort hash ignoring one hash level
by johngg (Canon) on May 14, 2012 at 15:25 UTC |