[Wellington-pm] Sum in one line

Jacinta Richardson jarich at perltraining.com.au
Mon Feb 28 19:13:07 PST 2005


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

-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
  (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Wellington-pm mailing list