It does look temptingly simple doesn't it?
But you'd be iterating across the list three times - which isn't very efficient. You only really need to iterate across it once. Something like this (off the top of my head and untested).
# use 'mean' instead of 'avg' as it's unambiguous sub min_max_mean { my ($min, $max, $tot); my $count = @_; $min = $max = $tot = shift; foreach (@_) { $min = $_ if $_ < $min; $max = $_ if $_ > $max; $tot += $_; } return ($min, $max, $tot / $count); }
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re^2: minimum, maximum and average of a list of numbers at the same time
by davorg
in thread minimum, maximum and average of a list of numbers at the same time
by LucaPette
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |