JP van Oosten

Perl oneliner to sum all lines

Jun 15, 2008

As I’m not such a big Perl-star, here is my first oneliner! This one sums all integers, presented at separate lines.

perl -e 'for (<STDIN>) { $sum += $_; } print $sum . "\n";'

(I didn’t know I could leave the $sum = 0; out, but someone with more perl knowledge pointed this out to me)

Very handy!