> Bryan Harris wrote:
>>
>>> chen li wrote:
>>>
>>>> I want to calculate the mean of an array. I know how
>>>> to let the job done by using a loop. But I just wonder
>>>> if Perl has the short-cut/built-in function for this.
>>>>
>>>> my @data=(1,1,1);
>>>>
>>>> mean=(1+1+1)/3=1;(Any perl built-in function for
>>>> this?)
>>> my $mean = do { my $s; $s += $_ for @data; $s / @data };
>>
>> I can't believe I just beat John in perl golf, by 14 strokes!
>>
>> (Anyone who knows me knows I just got lucky on that hole...)
>
> Well, if you want to golf that:
>
> my$mean=(map$a+=$_/@data,@data)[-1];
Looks like a tie now! Neat.
I wonder (only a little) which way is more efficient...
- B
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>