Your average specifically checks if no arguments are provided, but doesn't actually handle that situation. Fixed:
sub average { if (@_) { my $sum; for (@_) { $sum += $_; } return $sum/@_; } else { return undef; } }
Same, but shorter:
sub sum { my $sum; $sum += $_ for @_; $sum } sub avg { @_ ? sum(@_)/@_ : undef }
In reply to Re^2: resetting a foreach loop!
by ikegami
in thread resetting a foreach loop!
by lunette
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |