Front page | perl.beginners |
Postings from December 2002
RE: Sorting hash "values"
Thread Previous
|
Thread Next
From:
Wagner, David --- Senior Programmer Analyst --- WGO
Date:
December 30, 2002 20:55
Subject:
RE: Sorting hash "values"
Message ID:
113A7A0D1F47D511B92E00D0B7E03DAB033B2662@pmail02.vikingfreight.com
I takes the keys from the %hash and uses them to compare the the
data within the hash and since it is $b vs $a and not $a vs $b it is in
descending vs ascending sequence. For each key in %hash it then gets
assigned to key and you now have the key and data sored by value of the hash
and not the key.
Wags ;)
-----Original Message-----
From: Rajendra Babu, Praveen
[mailto:Praveen.RajendraBabu@acnielsen.com.au]
Sent: Monday, December 30, 2002 16:29
To: 'Mark Anderson'; beginners@perl.org
Subject: RE: Sorting hash "values"
Hello /\/\ark,
Thanks for your reply. The below code does exactly what I wanted to do
and guess where did I get it ?! - perldoc -f sort !!!
%hash = (
"h" => 100,
"a" => 200,
"z" => 50,
"b" => 600
);
foreach $key ( sort { $hash{$b} <=> $hash{$a} } keys %hash )
{
print "The key is $key \t";
print $hash{$key}, "\n";
}
But I can't really get the way the below statement works, esp. the using of
subroutine by "sort" function:
"foreach $key ( sort { $hash{$b} <=> $hash{$a} } keys %hash )"
Can someone please explain ???
-Praveen
-----Original Message-----
From: Mark Anderson [mailto:manderson@gcamail.com]
Sent: Tuesday, 31 December 2002 10:23 AM
To: Rajendra Babu, Praveen; beginners@perl.org
Subject: RE: Sorting hash "values"
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"}
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
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************
Thread Previous
|
Thread Next