Perl-Sensitive Sunglasses PerlMonks

### Comment on

 Need Help??

By testing eof() instead of eof in dave0's one-liner, you can extend its functionality to sum across multiple files. Like this:

```# First we make to test files
% perl -le 'print join(",", map int(rand(1000)), 1..10) for 1..50' > i
+n1.csv
% perl -le 'print join(",", map int(rand(1000)), 1..10) for 1..50' > i
+n2.csv
94,434,249,267,649,367,572,579,498,369
452,735,420,543,832,198,28,86,67,382
801,339,978,859,85,719,758,89,191,377

# Now we run dave0's original script on them
perl -lanF, -e '\$sum += \$F[6]; print "Sum is \$sum" if eof' in1.csv in2
+.csv
Sum is 23789
Sum is 48874

# Now let's try a small change --------------------------.
#                                                        |
#                                                        V
perl -lanF, -e '\$sum += \$F[6]; print "Sum is \$sum" if eof()' in1.csv i
+n2.csv
Sum is 48874
What happens is that eof (no parens!) returns true at the end of each file in @ARGV, while eof() is true only at the end of the last one (see eof). In the case of a single input file, the behavior is the same as before.

To get subtotals, you'd do

```perl -lanF, -e '\$total += \$F[6]; \$sub += \$F[6]; print "Subtotal: \$sub"
+ and \$sub = 0 if eof; print "Total: \$total" if eof()' in1.csv in2.csv
Subtotal: 23789
Subtotal: 25085
Total: 48874

An alternative to testing for eof(), is to put everything in an END. e.g.

```perl -lanF, -e '\$sum += \$F[6]; END{ print "Sum is \$sum" }' in1.csv in2
+.csv

Update: Bug in last one-liner corrected.

the lowliest monk

In reply to Re: Efficient way to sum columns in a file by tlm
in thread Efficient way to sum columns in a file by sk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-02-26 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (315 votes). Check out past polls.

Notices?