Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sort Hash of Hash numerically

by zek152 (Pilgrim)
on Jun 13, 2011 at 14:36 UTC ( [id://909391]=note: print w/replies, xml ) Need Help??


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 (Archbishop) 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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909391]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2026-01-21 15:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (125 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.