CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sort hash of hashes by value

 



britantyo
Novice

Aug 10, 2010, 7:35 AM

Post #1 of 2 (725 views)
sort hash of hashes by value Can't Post

Hey there, I come again with some problems...again (damn i'm lame) Frown okay, here it is :

i was parsing some raw data and put it in HoH, but after that, i want to sort them out. here is some example of my HoH

Code
%HoH =  
(
196,
{
BILLPAYMENT => { AMOUNT => 96666480, FREQ => 41 },
BRANCH_ID => 140,
INQUIRY => { AMOUNT => 0, FREQ => 141 },
TOTFREQ => 250,
TRANSFER => { AMOUNT => 50899474, FREQ => 20 },
WITHDRAWAL => { AMOUNT => 233300000, FREQ => 189 },
WITHDRAWALUSD => {},
},
242,
{
BILLPAYMENT => { AMOUNT => 235892855, FREQ => 59 },
BRANCH_ID => 200,
INQUIRY => { AMOUNT => 0, FREQ => 190 },
TOTFREQ => 225,
TRANSFER => { AMOUNT => 145124400, FREQ => 42 },
WITHDRAWAL => { AMOUNT => 82750000, FREQ => 124 },
WITHDRAWALUSD => {},
},
)


the "TOTFREQ" is the sum of all type "FREQ". now i just want to sort them out base on "TOTFREQ"'s value.

FYI : the key of this HoH is 196 and 242. they're unique.

so please perlguru, help me with my problem here, i have nobody to ask about perl around me T_T . so pleeeeeease perlguru help me
Be free to decide your dream, Put the details of your dream in your head and heart And don't give up 'till you drop dead.


shawnhcorey
Enthusiast


Aug 10, 2010, 8:11 AM

Post #2 of 2 (722 views)
Re: [britantyo] sort hash of hashes by value [In reply to] Can't Post


Code
my @sorted_keys = sort { $HoH{$a}{TOTFREQ} <=> $Hoh{$b}{TOTFREQ} } keys %HoH;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives