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, 2320👍, 0💬
Popular Posts:
How To Enter Boolean Values in SQL Statements? - MySQL FAQs - Introduction to SQL Basics If you want...
If locking is not implemented what issues can occur? IFollowing are the problems that occur if you d...
Can you tell me how to check whether a linked list is circular? Create two pointers, and set both to...
Can Sub Procedure/Function Be Called Recursively? - Oracle DBA FAQ - Creating Your Own PL/SQL Proced...
Is XHTML Element Name Case Sensitive? - XHTML Tutorials - Introduction To Tag and Attribute Syntax Y...