The "little extra work" includes keeping the resulting array sorted. Otherwise, there's no way to detect which is currently the smallest value, and which one needs replacing. Only $n items need to be printed, and if $n is small enough, I can imagine that perl's built-in sort will do well enough. As the data is sorted, you can try insert a new value at the appropriate place yourself, but the overhead of doing this in pure perl might not be worth it.

Also, personally, I'd prefer to keep more than $n items if there's a tie for the lowest value of those to keep. For example, if you need the top 3 values and the top 5 hash values are (1, 2, 4, 4, 5), I'd actually keep the top 4: (1, 2, 4, 4). So here's some code that implements that. I also added lots of reporting code, showing exactly what's going on.
Feel free to "optimize" it.

my %HASH; foreach('AA' .. 'ZZ') { $HASH{$_} = 1 + int rand 1000; } my $keep = 20; my @top; if(scalar keys %HASH <= $keep) { # You actually need to show them all. @top = sort { $b->[1] <=> $a->[1] } map [$_, $HASH{$_}], keys %HAS +H; } else { my $threshold; while(my($key, $value) = each %HASH) { if(@top < $keep) { printf "Adding %s => %d\n", $key => $value; push @top, [$key, $value]; $threshold = $value if !defined $threshold or $value > $th +reshold; } elsif($value > $threshold) { @top = sort { $b->[1] <=> $a->[1] } @top, [$key, $value]; printf "Inserting %s => %d\n", $key => $value; $threshold = $top[$keep-1][1]; printf "New threshold: %d\n", $threshold; while($top[-1][1] < $threshold) { printf "Popping %s => %d\n", @{$top[-1]}; pop @top; } printf "Keeping %d items\n", scalar @top; } elsif($value == $threshold) { printf "Adding %s => %d\n", $key => $value; push @top, [$key, $value]; } } } use Data::Dumper; $Data::Dumper::Terse = 1; print Dumper \@top;

In reply to Re: Re: *Fastest* way to print a hash sorted by value by bart
in thread *Fastest* way to print a hash sorted by value by smellysocks

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.