Octavian Rasnita wrote:
Gunnar Hjalmarsson wrote:

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;

I have tried that, but it gave me an error telling that $year is undefined.

Well, since you introduced that variable yourself, I assumed you had set it.

$year should be a variable but the sort function should loop
through all the years for every code of all the companies, and
return the biggest profit.

That's why I think I need to use a second hash that uses the code
of the companies as a hash key and the profit as its values.

Then I tend to agree. Actually you want to do several sorts, not just one. Something like:

    my %maxprofit;
    for ( keys %comp ) {
        my $year = ( sort {
          $comp{$_}{$b}{profit} <=> $comp{$_}{$a}{profit}
        } keys %{ $comp{$_} } )[0];
        $maxprofit{$_} = $comp{$_}{$year}{profit};
    }

--
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