Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have following data structure:

%scores =( Terry =>{science => 96, Math =>98, History =>92}, John =>{ science =>92, Math => 99, History =>89}, Lee=>{Science=>99,Math=>100,History=>94}, );
I want to print Totals for individual in sorted order & for each student their scores should be sorted.

Added code tags 2002-01-31 -- dvergin

Replies are listed 'Best First'.
Re: how do i sort values of a hash of hash
by theguvnor (Chaplain) on Jan 31, 2002 at 05:14 UTC
    The Camel has a discussion of sorting a hash by values rather than the keys. You use the optional expression block in the sort function.

    @sortedkeys = sort { $hash{$a} <=> $hash{$b} } keys %hash;

    Good luck.

    Extrapolate the above to what you need...

Re: how do i sort values of a hash of hash
by Fletch (Bishop) on Jan 31, 2002 at 05:17 UTC

    • <code> tags: learn them, live them, love them.
    • perldoc -q "sort a hash"
    • Fish:
      my @by_stud = map { my $t = $scores{$_}; [sort { $t->{$b} <=> $t->{$a} } keys %{$t}] } keys %scores;

    Update: Actually, I just checked perldoc perldsc and it gives almost an exact example of sorting a similar HoH. Search for Access and Printing of a HASH OF HASHES.