chinamox has asked for the wisdom of the Perl Monks concerning the following question:
greetings all,
I am trying to go the extra mile on some classwork and make it so that my program can output processed data as a hash that is sorted by its values rather than its keys.
I am looking to end up with something like this:
Homer is 37 years old. Marge is 36 years old. Bart is 12 years old. Lisa is 9 years old. Maggie is 2 years old.
Ok, I know you can't actually sort a hash. Instead I am instead trying to pass my results into an array, use the sort function to organize the array. The goal would be to be able to use the program to sort any similar hashes. (Think %Jetsons and %Scoobydoo)
So far I have this:
#first the example %hash my %age = ( "Maggie" => 2, "Homer" => 37, "Bart" => 12, "Lisa" => 9, " +Marge" => 36); #Setting up and sorting the array... my @simpson_age = sort by_age keys %age; #I snagged this is From O'reilly's Learning Perl, 3rd ed... sub by_age { $age{$b} <=> $age{$a} }
I feel that I am close - but so far, no dice! Any tips on where to go from here?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting a hash by values...
by McDarren (Abbot) on Oct 09, 2006 at 17:34 UTC | |
by blueberryCoffee (Scribe) on Oct 09, 2006 at 17:57 UTC | |
by chinamox (Scribe) on Oct 10, 2006 at 09:27 UTC | |
|
Re: Sorting a hash by values...
by chargrill (Parson) on Oct 09, 2006 at 16:28 UTC | |
by chinamox (Scribe) on Oct 10, 2006 at 09:14 UTC | |
|
Re: Sorting a hash by values...
by johngg (Canon) on Oct 09, 2006 at 22:51 UTC | |
by chinamox (Scribe) on Oct 10, 2006 at 09:30 UTC |