chen li wrote:
> Dear all,

Hello,

> Thank for replying my post. Here is the summary of all
> the posssible code lines:
> 
> Q:If Perl has the short-cut/built-in function for
> calculating Mean/Average?
>  
>  my @data=(1,1,1); 
>  
>  mean/average=(1+1+1)/3=1;
> 
> A: 
> 
> No Perl built-in function for mean/average but there
> are several ways to do it:
> 
>  1) my $mean = do { my $s; $s += $_ for @data; $s /
> @data };
>  
>  2)List::Util is "built-in" as of 5.8, and back
> compatible to 5.5 
> 
>   use List::Util qw(sum);
> 
>   my $average = sum(@input) / @input;
> 
> 3) my $mean= (map$a+=$_/@data,@data)[-1];
> 
> 
> 4)my $mean = do { my $s; $s += $_ for @data; $s /
> @data };

1 and 4 are exactly the same.


> 5) my $mean+=$_/@data [EMAIL PROTECTED];

That was not posted and it won't work correctly (see the NOTE at the end of
the "Statement Modifiers" section of perlsyn.)


> 6)$mean = eval(join("+", @data)) / @data;
> 
> Depending on how you understand Perl and what progress
> you are I prefer 6).

Depending on how you understand Perl, 6 is the worst solution.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to