[Wellington-pm] Sum in one line

Andrew Ruthven andrew.ruthven at catalyst.net.nz
Mon Feb 28 19:25:23 PST 2005


[Now that this email address is subscribed to the list I'll try again...]

Or if you like to use map we have:

my $total;
map $total += $_, @numbers;

(Which is one character longer than Jacinta's.  Damn.)


On Tue, 2005-03-01 at 14:13 +1100, Jacinta Richardson wrote:
> Finlay Thompson wrote:
> 
> > Anyway, does anyone know of a good oneliner ? The best I can do is:
> > 
> >   my $total = sub {my $s = 0; $s += $_ for @_; $s }->(@numbers);
> 
> 	$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
> 
-- 
Andrew Ruthven, Wellington, New Zealand
Catalyst IT Limited --> http://www.catalyst.net.nz
At work: andrew.ruthven at catalyst.net.nz
At home: andrew at etc.gen.nz
GPG fpr: 34CA 12A3 C6F8 B156 72C2  D0D7 D286 CE0C 0C62 B791


More information about the Wellington-pm mailing list