Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: Sorting keys of hash table by values

by lidden (Curate)
on Jan 31, 2008 at 23:30 UTC ( #665486=note: print w/replies, xml ) Need Help??


in reply to Re^4: Sorting keys of hash table by values
in thread Sorting keys of hash table by values

2000 and 5000 keys, 1000 and 3000 iterations.

Code:

srand 0; my @letters = split //, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS +TUVWXYZ'; sub rnd_string{ my $string; for( 1 .. (5 + int rand 10)){ $string .= $letters[ rand @letters ]; } #say $string; return $string; } my %hash; for(1 .. 5_000){ $hash{rnd_string()} = rand; } my @sorted_keys; for(1..3_000){ #@sorted_keys = sort { $hash{$b} <=> $hash{$a} } keys %hash; @sorted_keys = reverse sort { $hash{$a} <=> $hash{$b} } keys %hash +; #@sorted_keys = sort { -( $hash{$a} <=> $hash{$b} ) } keys %hash; } say scalar @sorted_keys;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://665486]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2023-11-29 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?