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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |