To simplify on the above solutions...
You can use the closure method, but that is really making a new sort function for each hash you have.
What you want is a function that takes a hash, and returns the sorted keys.
sub sorted_by_dept(\%) {
my $href = shift;
my @sorted = map {$_->[0] }
sort {$a->[1] <=> $b->[1] }
map { [$_, $href->{$_}->{dept}] }
keys %$href;
return @sorted;
}
To break this down, follow it backwards. Starting at keys -- for each key in the hash, the last map builds pairs of values. The first number of the pair is the end value you want. The second number of the pair is what you want to sort by.
The sort command sorts by the second number, and the upper map looks up the original key value for you.
You then have a sorted list of keys you can use in a foreach.
Hope this helps,
~jmanning2k
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.