Or more verbose, but more understandable (without delving into the actual expansion of -n)
perl -lne '$sum+=$_; END{print $sum}'
The END block is executed after everything else. You can also use a corresponding BEGIN block before anything else runs, and there are others (CHECK and INIT, having to do with compile time) for fancy stuff. | [reply] [d/l] [select] |
| [reply] [d/l] [select] |
perl -lpe '$-+=$_}{$_+=$-}{'
| [reply] [d/l] |
| [reply] [d/l] |
update - whoops
I just saw his tail -1 at the end, he is looking for a final total not a running one, DOH!. So option 2 below works just fine
Original dumb post
This does not work for me, is this my old perl (5.005_03) or am I doing something wrong ? If I add a continue it will work. Here is some output, I have added the > before the numbers I entered just for clarity.
:perl -lne "$sum+=$_}{print $sum"
syntax error at -e line 1, near "+="
Execution of -e aborted due to compilation errors.
:perl -lne '$sum+=$_}{print $sum'
>1
>2
>3
:perl -lne '$sum+=$_}continue{print $sum'
>1
1
>2
3
>3
6
>4
10
Cheers, R. | [reply] [d/l] |