Well (d'oh), an important thing I forgot to mention is that I also need to know where I am in the $HoH for any given value. So I not only need to find the top x (10 in my case) values of $HoH->{$foo}{$bar}, but I also need to keep track of the value of $foo and $bar for each of those x values.

Anyway, your code gave me some idea and this is what I've come up with for now.
while (my $kid_ = $kills_sth->fetchrow_hashref()){ $KP->{$kid_->{k}}{$kid_->{v}}++; } # How many do we keep? my $total = 10; my %top; for(keys %{$KP}){ # Player ID my $pid = $_; VAL: for ( keys %{$KP->{$pid}}) { # Target ID my $tid = $_; my $frags = $KP->{$pid}{$tid}; for(0 .. ($total - 1)){ my $idx = $_; $top{$idx}{f} ||= 0; if(($idx != ($total - 1)) && $frags > $top{$idx}{f}){ my $idx2 = $idx + 1; $top{$idx2}{k} = $top{$idx}{k}; $top{$idx2}{v} = $top{$idx}{v}; $top{$idx2}{f} = $top{$idx}{f}; $top{$idx}{k} = $pid; $top{$idx}{v} = $tid; $top{$idx}{f} = $frags; next VAL; } elsif(($idx == ($total - 1)) && ($frags > $top{$idx}{f})){ $top{$idx}{k} = $pid; $top{$idx}{v} = $tid; $top{$idx}{f} = $frags; next VAL; } } } } for(0 .. ($total - 1)){ my $id = $_; printf("%2s. p: %s\tt: %s\tf: %s\n",($id+1),$top{$id}{k},$top{$id} +{v},$top{$id}{f}); }
Still, it seems awfully inefficient to me and I'm really leary of it's ninja scaling abilities. But that said, it works and produces the intended results:
1. p: 2838413 t: 3440380 f: 328 2. p: 2838413 t: 1261188 f: 282 3. p: 539960 t: 2976790 f: 273 4. p: 3440380 t: 2838413 f: 268 5. p: 182560 t: 53957 f: 206 6. p: 539960 t: 53957 f: 196 7. p: 1261188 t: 53957 f: 190 8. p: 539960 t: 3440380 f: 171 9. p: 3440380 t: 539960 f: 146 10. p: 53957 t: 1261188 f: 127

I'd appreciate any ideas about increasing efficiency.

Thanks a bunch for your help.

ps. Yes, it's a half-life stats program. :)

In reply to Re: Re: Re: Sorting values of nested hash refs by doran
in thread Sorting values of nested hash refs by doran

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.