Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

How do I sort a hash by its values?

by Anonymous Monk
on Feb 22, 2000 at 15:14 UTC ( #3860=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash containing error-codes from a web-server log, and a number as values. I would like to print them out sorted by the number. How do I do that?

Replies are listed 'Best First'.
Re: How do I sort a hash by its values?
by btrott (Parson) on Feb 22, 2000 at 22:38 UTC
    Read perlman:perlfaq4, "How do I sort a hash (optionally by value instead of key)?"

    But in short:

    @keys = sort { $hash{$b} <=> $hash{$a} } keys %hash;
    @keys now contains the sorted list of keys.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://3860]
Approved by root
[shmem]: protloose? can't parse that
[choroba]: neither can I
[choroba]: and the first Google hit is tagged "human feces" :-o
[choroba]: oh, missed the double o
[shmem]: "brotlos"?
[shmem]: heavy protein discharge?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-08-17 09:57 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (286 votes). Check out past polls.