in reply to Re: resetting a foreach loop!
in thread resetting a foreach loop!
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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: resetting a foreach loop!
by pryrt (Abbot) on Nov 17, 2017 at 19:36 UTC | |
by ikegami (Patriarch) on Nov 17, 2017 at 20:00 UTC | |
by AnomalousMonk (Archbishop) on Nov 17, 2017 at 20:15 UTC | |
by ikegami (Patriarch) on Nov 18, 2017 at 00:07 UTC | |
by AnomalousMonk (Archbishop) on Nov 18, 2017 at 00:43 UTC | |
|