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

Not sure if the original poster understood, but I'm still a little lost, could you go into more detail?

Kickstart

Replies are listed 'Best First'.
Re: Re: sorting hash of hashes
by suaveant (Parson) on Apr 24, 2001 at 21:34 UTC
    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