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

Re: Sorting hash "values"

Thread Previous | Thread Next
From:
Dave K
Date:
December 30, 2002 16:06
Subject:
Re: Sorting hash "values"
Message ID:
20021231000630.5920.qmail@onion.perl.org
$ perl -e '
%hash = (
         "h" => 100,
         "a" => 2000,
         "z" => 50,
         "b" => 600
        );
for(sort { $hash{$b} <=> $hash{$a} } keys %hash) {
print $hash{$_}, " = $_\n";
}'
2000 = a
600 = b
100 = h
50 = z

HTH
"Rajendra Babu" <Praveen.RajendraBabu@acnielsen.com.au> wrote in message
news:E73ABEF47B1FD51192C300D0B7B9D19D064D023B@acnsydmsx15.acnielsen.com.au..
.
>
> %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.......
>
> TIA,
> Praveen
>
> IMPORTANT-
> (1) The contents of this email and its attachments are confidential and
> intended only for the individual or entity named above. Any unauthorised
> use of the contents is expressly prohibited.  If you receive this email
> in error, please contact us, then delete the email.
> (2) ACNielsen collects personal information to provide and market our
> services. For more information about use, disclosure and access see our
> privacy policy at <www.acnielsen.com.au> or contact us on
privacyofficer@acnielsen.com.au



Thread Previous | Thread Next


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