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

RE: Sorting hash "values"

Thread Previous
From:
Mark Anderson
Date:
December 30, 2002 15:23
Subject:
RE: Sorting hash "values"
Message ID:
POENIDJMGJHIICHDIHNPGEHIDFAA.manderson@gcamail.com
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

				/\/\ark

-----Original Message-----
From: Rajendra Babu, Praveen
[mailto:Praveen.RajendraBabu@acnielsen.com.au]
Sent: Monday, December 30, 2002 3:16 PM
To: 'beginners@perl.org'
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
documentation.

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


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About