This is a discussion on How do I sort a hash by the hash value? within the Perl forums, part of the Software Development category; Hi buddies... can any one tell me...How do I sort a hash by the hash value?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Hi buddies... can any one tell me...How do I sort a hash by the hash value?
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
|
#2
| |||
| |||
| Here's a program that prints the contents of the grades hash, sorted numerically by the hash value. Check it out… Code: #!/usr/bin/perl -w
# Help sort a hash by the hash 'value', not the 'key'.
to highest).
sub hashValueAscendingNum {
$grades{$a} <=> $grades{$b};
}
# Help sort a hash by the hash 'value', not the 'key'.
# Values are returned in descending numeric order
# (highest to lowest).
sub hashValueDescendingNum {
$grades{$b} <=> $grades{$a};
}
%grades = (
student1 => 90,
student2 => 75,
student3 => 96,
student4 => 55,
student5 => 76,
);
print "\n\tGRADES IN ASCENDING NUMERIC ORDER:\n";
foreach $key (sort hashValueAscendingNum (keys(%grades))) {
print "\t\t$grades{$key} \t\t $key\n";
}
print "\n\tGRADES IN DESCENDING NUMERIC ORDER:\n";
foreach $key (sort hashValueDescendingNum (keys(%grades))) {
print "\t\t$grades{$key} \t\t $key\n";
} |
|
#3
| |||
| |||
| Refer this URL:- Learn Perl: Perl Q&A: How do I sort a hash by the hash "value"?
__________________ Regards, VELHARI I am not totally useless. I can be used for a bad example |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to sort Datatable | oxygen | C# Programming | 2 | 04-22-2008 09:25 PM |
| How can we sort the values in dataset? | mobilegeek | C# Programming | 4 | 04-18-2008 03:39 AM |
| Overview of Hash table. | H2o | Other Web Programming Languages | 1 | 09-14-2007 06:28 AM |
| What’s the difference between Hash Map and Hash Table? | mobilegeek | Java Programming | 2 | 09-14-2007 05:37 AM |
| What is a hash? | vadivelanvaidyanathan | Perl | 1 | 07-30-2007 06:29 AM |
Our Partners |