in reply to calculate average from range of hash values (optimize)
I don't see the need to sort all the keys in %{$data}. You can use hash slices and List::Utils::sum() to good advantage.
use List::Utils 'sum'; sub average { my ($data, $first, $last) = @_; my %data = %$data; my @goodkeys = grep {exists $data{$_}} $first .. $last; sum( @data{ @goodkeys } ) / @goodkeys; }
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: calculate average from range of hash values (optimize)
by Aristotle (Chancellor) on Jul 27, 2003 at 02:12 UTC | |
by demerphq (Chancellor) on Jul 27, 2003 at 13:59 UTC | |
by Aristotle (Chancellor) on Jul 27, 2003 at 18:32 UTC |