develooper Front page | perl.beginners | Postings from December 2002

RE: Sorting hash "values"

Thread Previous
Mark Anderson
December 30, 2002 15:23
RE: Sorting hash "values"
Message ID:
It helps to answer your question if you show us what you've already done,
and tell us what you know...

perldoc -f keys
perldoc -f sort


-----Original Message-----
From: Rajendra Babu, Praveen
Sent: Monday, December 30, 2002 3:16 PM
To: ''
Subject: Sorting hash "values"

%hash = (
         "h" => 100,
         "a" => 200,
         "z" => 50,
         "b" => 600

For the above %hash, I want to sort them by "values"(rather than keys) and
print-out the sorted key-value pair. Can it be done ??
The output needs to look like the below:

b  600
a  200
h  100
z  50

  Your esteemed ideas/suggestions please.......

-----End Original Message-----

See top of message (if you haven't already) for pointers to relevant

for my $key (sort {$hash{$b} <=> $hash{$a}} keys %hash) {print

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About