[Wellington-pm] ? shorten an Perl snippet

pfCd?Y*_Qbdb6 at perform.shyuser.com pfCd?Y*_Qbdb6 at perform.shyuser.com
Mon Aug 2 21:43:03 PDT 2010


Grant's  snippet squeezed & clutter happily nixed.




On 8/2/2010 6:27 PM, pfCd?Y*_Qbdb6 at perform.shyuser.com wrote:
> Hmmm.  I'll try getting into that.
> That's enough for the moment.
>
>
>
> On 8/2/2010 5:52 PM, Grant McLean wrote:
>> On Tue, 2010-08-03 at 09:34 +1200, Dan Horne wrote:
>>> Without wanting to golf it, perhaps something like
>>>
>>> my $product = 0;
>>>
>>> for my $j (10 .. 59) {
>>> $product += $split_line[$j][5];
>>> }
>>>
>>> $product = $product/50;
>>>
>>> You could divide by 50 inside the loop, but that might
>>> introduce too
>>> many rounding errors
>>
>> That's certainly an improvement over the original code.
>>
>> I'd also be inclined to put the code in a subroutine with a
>> name that
>> made it clear what it was doing. Also the List::Util module
>> (which has
>> been included with Perl since 5.8) provides a few tools you
>> could use,
>> for example:
>>
>> use List::Util qw(sum);
>>
>> $product1 = average_50d(@split_line);
>>
>> sub average_50d {
>> my $sum = sum map { $_->[5] } @_[10..59];
>> return $sum / 50;
>> }
>>
>> For that quantity of data it would be more efficient to pass
>> it by
>> reference but I didn't want to clutter up the example.
>>
>> Cheers
>> Grant
>>
>> _______________________________________________
>> Wellington-pm mailing list
>> Wellington-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/wellington-pm
>>
> _______________________________________________
> Wellington-pm mailing list
> Wellington-pm at pm.org
> http://mail.pm.org/mailman/listinfo/wellington-pm
>


More information about the Wellington-pm mailing list