[Wellington-pm] Sum in one line
Finlay Thompson
finlay at catalyst.net.nz
Mon Feb 28 19:33:36 PST 2005
However, to do it anonymously ?
for example as the argument to another routine :
my $total; map $total += @numbers;
my $pretty_sum = format_number($total);
still seems a bit long ....
I wish there was a routine, so you could do it without having to declare
another variable.
my $pretty_sum = format_number(sum @numbers);
There is such a thing in the module:
use List::Util qw(sum);
Finlay
Finlay Thompson wrote:
> thanks , thats much better!
>
>
>
> Jacinta Richardson wrote:
>
>>
>> Finlay Thompson wrote:
>>
>>> Anyway, does anyone know of a good oneliner ? The best I can do is:
>>>
>>> my $total = y;
>>
>>
>>
>> $total += $_ for @numbers;
>>
>> of course if you're defining $total as well, it's a little harder.
>> Perhaps:
>>
>> my $total; $total += $_ for @numbers;
>>
>> strictly speaking it's two lines, but it's shorter than your
>> suggestion. ;)
>>
>> Good question!
>>
>> All the best,
>>
>>
>> Jacinta
>>
>
> _______________________________________________
> 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