in reply to Re: Get average value of array of numbers
in thread Get average value of array of numbers
Dividing by @_ (which in scalar context gives the number of elements) returns the average.What happens if the array is empty (0 elements)?
Maybe:
Update: Per oha: What is the proper result of averaging zero elements? 0? undef? A divide-by-zero (or some other) exception? Something else? Any of these results can easily be supported. I wanted to make the point that this boundary condition should at least be considered.>perl -wMstrict -le "use List::AllUtils qw(sum); sub mean { return @_ ? sum(@_) / @_ : 0 } my @Array = (3, 5, 7, 12,24); print mean(@Array); print mean(); " 10.2 0
|
|---|