in reply to Sort Hash of Hash numerically

Basically you have to store the data in a temporary hash. Keep the family name with the subhash and sort the array by the head count.

#insert HoH here @temp_array = (); for $i (keys(%HoH)) { #the name must be kept with the subhash. $HoH{$i}{"name"} = $i; push @temp_array, $HoH{$i}; #updated:transposition error on my part } #sort the temp array by decreasing head counts @temp_array = sort {$$b{"head_count"} <=> $$a{"head_count"}} @temp_arr +ay; for $i (@temp_array) { print "$$i{'name'} House Type: $$i{'house_type'} Head Count $$i{'h +ead_count'}\n"; }

Updated: error in my code.

Replies are listed 'Best First'.
Re^2: Sort Hash of Hash numerically
by AnomalousMonk (Bishop) on Jun 13, 2011 at 17:03 UTC
    for $i (keys(%HoH)) { #the name must be kept with the subhash. $HoH{$i}{"name"} = $i; push @temp_array, $HoH }

    In the above for loop, the statement
        push @temp_array, $HoH
    would appear to need to be
        push @temp_array, $HoH{$i};
    to work (or to compile with strictures).