Search code examples
perlsortingloopsprintinghash-of-hashes

accessing the highest value in hash of an hash in perl?


so i have a hash of an hash that looks something like this:

my %hash = (
    'fruits' => {
        'apple'    => 34,
        'orange'   => 30,
        'pear'     => 45,
    },
    'chocolates' => {
        'snickers' => 35,
        'lindt'    => 20,
        'mars'     => 15,
    },
);

I want to access only the fruit which is max in number and chocolate which is max in number. the output should look like:

fruits: pear chocolates : snickers

foreach my $item (keys %hash){
#print "$item:\t"; # this is the object name
foreach my $iteminitem (keys %{$hash{$item}})
    {
    my $longestvalue = (sort {$a<=>$b} values %{$hash{$item}})[-1]; #this stores the longest value
     print "the chocolate/fruit corresponding to the longestvalue" ;   

     #iteminitem will be chocolate/fruit name
    }
 print "\n";
}

I know it is not difficult but I am blanking out!


Solution

  • The following sorts the keys of each hashref by descending value, so the max is the first element returned:

    my %hash = (
        chocolates => { lindt => 20, mars => 15, snickers => 35 },
        fruits     => { apple => 34, orange => 30, pear => 45 },
    );
    
    while (my ($key, $hashref) = each %hash) {
        my ($max) = sort {$hashref->{$b} <=> $hashref->{$a}} keys %$hashref;
        print "$key: $max\n";
    }
    

    Outputs:

    fruits: pear
    chocolates: snickers