Question: I am trying to find out maximum value in a hash and corresponding key to that maximum value. My hash looks like 

```
%hash = (
    bob => "4.9",
    gita => "3.9 , 6.8",
    diu => "3.0",
);

```

Now I want to find the maximum value in that hash with the key it belongs. 

Output needed is 

```
gita 6.8  

```

I am trying to sort the values in `%hash` in ascending order to get the maximum value like this 

```
sub hashValueAscendingNum {
    $hash{$a} cmp $hash{$b};
}

foreach my $highest (sort hashValueAscendingNum(keys(%hash))) {
    print "\t $hash{$highestMagnitude} \t\t $highest \n";
}

```

I want all the values in the hash to be checked, and the one with maximum value should be returned with its key. 

How can i do that?

Answer: 
You need to first associate each key with the highest of the values corresponding to it in the original `%hash` and then find the key associated with the highest value.

```
#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my %max = map { $_ => max @{ $hash{$_} } } keys %hash;

my ($argmax) = (sort { $max{$b} <=> $max{$a} } keys %max)[0];
my $max = $max{ $argmax };

print join(' => ', $argmax, $max), "\n";

```

Of course, this is very inefficient (esp. using `sort`) but for the dimensions you showed, it does not matter. For completeness, here is a more efficient version using `each`:

```
#!/usr/bin/env perl

use strict; use warnings;

use List::Util qw( max );

my  %hash = (
    bob => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu => [ 3.0 ],
);

my ($argmax, $max) = @{ init_argmax_max(\%hash) };

while (my ($k, $v) = each %hash) {
    $v = max @{ $v };
    if ( $v > $max ) {
        $argmax = $k;
        $max = $v;
    }
}

print join(' => ', $argmax, $max), "\n";

sub init_argmax_max {
    my ($hash) = @_;
    my ($argmax, $max) = each %{ $hash };

    keys %{ $hash };

    $max = max @{ $max };

    return [$argmax, $max];
}

```
```
#!/usr/bin/perl
use warnings;
use strict;

my %hash = (
    bob  => [ 4.9 ],
    gita => [ 3.9, 6.8 ],
    diu  => [ 3.0 ],
);

my $max_key;
my $max_val=0;

foreach my $key (keys %hash) {
    foreach my $val ( @{$hash{$key}} ) {
        ($max_key, $max_val) = ($key, $val)
            if $val > $max_val;
    }
}

print "$max_key => $max_val\n";

```