Aben Siatris wrote:
DÅa Tuesday 28 September 2004 11:42 Gunnar Hjalmarsson napÃsal:
Octavian Rasnita wrote:
I have a hash that have multiple sub hashes and I want to sort
it by a sub key.

For example, I have:

$comp{$code}{$year}{profit}.

And I want to sort the $code values after the value of the
"profit" key.

Is this possible directly, or I will need to create another
hash for doing this type of sort?

You don't need another hash.

    perldoc -f sort
    perldoc -q "sort a hash"

    my @codes = sort {
        $comp{$a}{$year}{profit}
                  <=>
        $comp{$b}{$year}{profit}
    } keys %comp;

my @codes = sort {$a <=> $b} keys %comp;

If I didn't misunderstand the OP completely, I can't see how that would do what he wants.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to