How do I sort a hash by the hash key?
Submitted by: AdministratorSuppose we have a class of five students.
Their names are kim, al, rocky, chrisy, and jane.
Here's a test program that prints the contents
of the grades hash, sorted by student name:
#!/usr/bin/perl -w
%grades = (
kim => 96,
al => 63,
rocky => 87,
chrisy => 96,
jane => 79,
);
print "ntGRADES SORTED BY STUDENT NAME:n";
foreach $key (sort (keys(%grades))) {
print "tt$key tt$grades{$key}n";
}
The output of this program looks like this:
GRADES SORTED BY STUDENT NAME:
al 63
chrisy 96
jane 79
kim 96
rocky 87
}
Submitted by: Administrator
Their names are kim, al, rocky, chrisy, and jane.
Here's a test program that prints the contents
of the grades hash, sorted by student name:
#!/usr/bin/perl -w
%grades = (
kim => 96,
al => 63,
rocky => 87,
chrisy => 96,
jane => 79,
);
print "ntGRADES SORTED BY STUDENT NAME:n";
foreach $key (sort (keys(%grades))) {
print "tt$key tt$grades{$key}n";
}
The output of this program looks like this:
GRADES SORTED BY STUDENT NAME:
al 63
chrisy 96
jane 79
kim 96
rocky 87
}
Submitted by: Administrator
Read Online Perl Programming Job Interview Questions And Answers
Top Perl Programming Questions
☺ | How do you give functions private variables that retain their values between calls? |
☺ | How many ways can we express string in Perl? |
☺ | How do you match one letter in the current locale? |
☺ | How do I set environment variables in Perl programs? |
☺ | How to open and read data files with Perl |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |