Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How do I sort a hash by the hash value?
How do I sort a hash by the hash value?
✍: Guest
Here's a program that prints the contents of the grades hash, sorted numerically by the hash value: #!/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"; }
2013-09-10, 2371👍, 0💬
Popular Posts:
What Happens If One Row Has Missing Columns? - XHTML 1.0 Tutorials - Understanding Tables and Table ...
How To Get the Uploaded File Information in the Receiving PHP Script? Once the Web server received t...
What Happens to Your Transactions When ERROR 1213 Occurred? - MySQL FAQs - Transaction Management: C...
Can include files be nested? The answer is yes. Include files can be nested any number of times. As ...
How To Enter Characters as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to ent...