#!/usr/bin/perl
use strict;
use warnings;
my %h = (
a=>{ a=>1, b=>2, c=>3, d=>4 },
b=>{ h=>5, i=>6, j=>5 },
c=>{ r=>5, s=>5, t=>5 },
e=>{ r=>5, s=>5, t=>4 },
);
for my $k (sort keys %h) {
my @t = kv_for_largest_v($h{$k});
print "Key $k: $t[0], $t[1]\n";
}
sub kv_for_largest_v {
my $hr = shift;
return undef unless keys %$hr;
my ($k,$v) = each %$hr;
while (my ($k2,$v2) = each %$hr) {
($k,$v) = ($k2,$v2) if $v2>$v;
}
return $k,$v;
}
####
my ($maxkey, $maxmax) = kv_for_largest_v($distances{$key1};
####
sub kv_for_largest_v {
my $hr = shift;
return undef unless keys %$hr;
my ($k,$v) = each %$hr;
my @rv = ([ $k, $v ]);
while (my ($k2,$v2) = each %$hr) {
if ($v2 > $rv->[0][1]) {
# New maximum value, reset the list
$rv = ([ $k2, $v2 ]);
}
elsif ($v2 == $rv->[0][1]) {
# Another copy of the max distance, add to the list
push @rv, [ $k2, $v2 ];
}
}
return \@rv;
}