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];

In reply to sort hash ignoring one hash level by AWallBuilder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.