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
In reply to Re: calculate average from range of hash values (optimize)
by Zaxo
in thread calculate average from range of hash values (optimize)
by revdiablo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |