Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
No edit summary |
Underscore (talk | contribs) (→{{header|Perl}}: General cleanup.) |
||
Line 266: | Line 266: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>sub avg { |
|||
@_ or return 0; |
|||
$sum = 0; |
my $sum = 0; |
||
foreach |
$sum += $_ foreach @_; |
||
return $sum/@_; |
|||
⚫ | |||
$count++; |
|||
} |
|||
return $count > 0 ? $sum / $count : 0; |
|||
⚫ | |||
print avg(qw(3 1 4 1 5 9)), "\n";</perl> |
|||
Output: |
|||
3.83333333333333 |
|||
{{libheader|Data::Average}} |
{{libheader|Data::Average}} |
||
With module Data::Average. |
With module Data::Average. |
||
(For zero-length |
(For zero-length arrays, returns the empty list.) |
||
<perl>use Data::Average; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Output: |
|||
⚫ | |||
3.83333333333333 |
|||
⚫ | |||
=={{header|Pop11}}== |
=={{header|Pop11}}== |