in reply to Re: Re^2: calculate average from range of hash values (optimize)
in thread calculate average from range of hash values (optimize)

For the record your second solution corrupts the hash with keys whose value is undef.
Eh? Extraordinarily strange. So a slice lookup autovivifies elements, while a single-element lookup doesn't. Feels like a bug, if you ask me.. certainly counterintuitive. Easy enough to fix, though.
use List::Util qw(sum); sub average { my ($data, $first, $last) = @_; my @values = map { local $_ = $data->{$_}; defined() ? $_ : () } $ +first .. $last; sum(@values) / @values; }

Makeshifts last the longest.