Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^4: calculate average from range of hash values (optimize)

by Aristotle (Chancellor)
on Jul 27, 2003 at 18:32 UTC ( #278252=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://278252]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2019-11-15 12:52 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (83 votes). Check out past polls.