in reply to Re: Sorting values of nested hash refs
in thread Sorting values of nested hash refs
Cheers - L~Rmy @vals = sort {$b <=> $a } map { values %{$hoh->{$_}} } keys %$hoh; my ($one, $two, $three) = @vals[0..2];
Update Completely missed that tinita had an almost identical solution below. In the case of a very large HoH, you can avoid sorting and gain a bit of efficiency with the following:
my @top_3; my $filled = 0; VAL: for my $val ( map { values %{$hoh->{$_}} } keys %$hoh ) { if ( ! $filled ) { for ( 0 .. 2 ) { if ( ! defined $top_3[$_] ) { $top_3[$_] = $val; $filled = 1 if $_ == 2; next VAL; } } } if ( $val > $top_3[0] ) { ($top_3[0], $top_3[1]) = ($val, $top_3[0]); } elsif ( $val > $top_3[1] ) { ($top_3[1], $top_3[2]) = ($val, $top_3[1]); } elsif ( $val > $top_3[2] ) { $top_3[2] = $val; } } print "$_\n" for @top_3;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Sorting values of nested hash refs
by doran (Deacon) on Mar 01, 2004 at 07:06 UTC | |
by Limbic~Region (Chancellor) on Mar 01, 2004 at 14:40 UTC |