Is it the keys or the values that are sorted? I will assume keys, but you can apply anything you like later:
update – ww pointed out that @k/$pct only works when $pct == 10. I feel sheepish, but I've corrected the code below. :-/
my @k = sort keys %the_hash; my $pct = 10; # what percent to print #- WRONG -# for ( 0..int(@k/$pct)-1 ) { for ( 0.. int( @k * ($pct/100) ) )-1 ) { printf "%s => %s\n", $k[$_], $the_hash{$k[$_]} }
Or, if you want to keep the top x% and discard the rest:
my @k = sort keys %the_hash; my $pct = 10; # what percent to keep #- WRONG -# for ( int(@k/$pct)-1..$#k ) { delete $the_hash{$k[$_]} } for ( int( @k * ($pct/100).. ) )-1..$#k ) { delete $the_hash{$k[$_]} } # Now %the_hash only contains the top 10%
In reply to Re: Selection of Hash key value pairs
by radiantmatrix
in thread Selection of Hash key value pairs
by Gavin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |