Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: sorting hash of hashes

by suaveant (Parson)
on Apr 24, 2001 at 21:34 UTC ( [id://75159]=note: print w/replies, xml ) Need Help??


in reply to Re: sorting hash of hashes
in thread sorting hash of hashes

Ok, so, when you do:
foreach my $symbol_set (sort keys %pnl_ind) {
it returns you a sort set of keys from the top level of the hash...
my $symbols_r=$pnl_ind{$symbol_set);
stores the reference to the second level hash element under the current $symbol_set (which is now in sorted order)
foreach my $symbol (sort ($symbols_r->{$a}{'pnl'}<=>$symbols_r->{$b}{' +pnl'}) keys %{$symbols_r} ) {
the keys %{$symbols_r} derefences the hash ref, so that you can go through the keys... he wants to sort through the symbol patrt of the hash by the value associated with the pnl key inside the symbol part of the hash. No, since the sort is looking at the keys of the symbol level of the hash, $a and $b hold keys, so you do $symbols_r->{$a}{'pnl'} to get the value of the pnl key and <=> it to the $symbols_r->{$b}{'pnl'}. Normally sort would compare $a and $b directly, but since you want the values inside the hash, you need to get the values out using $a and $b as keys, to get to the real sort data... so now the hash comes back sorted by symbol_set, and within symbol set you get symbols ordered by their pnl values (whatever a pnl value is)

make more sense?
                - Ant

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-18 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found