# ðŸŽ„ 23/25. Calculating totals with Perl 6

Welcome to Day 23 of the Perl 6 One-Liner Advent Calendar! End of the year is the time when people evaluate there year results, and Perl 6 can help with that, too.

Today, weâ€™ll see a one-liner that calculates totals for the columns of a table.

Hereâ€™s some sample data in a file:

`100.20  303.50 150.25130.44 1505.12  36.41200.12  305.60  78.12`

And hereâ€™s the code that prints three numbersâ€”totals for each column:

`put [Z+] lines.map: *.words`

The program prints the numbers that we need:

`430.76 2114.22 264.78`

From the update of yesterdayâ€™s post, we know that bare lines is the same as \$*IN.lines, so lines.map iterates over all the lines in the input stream. Each line is then split into wordsâ€”substrings separated by whitespaces.

The part of the job that parses input data is complete. We have got a number of sequences corresponding to the lines of input data. For our sample file, these are the following:

`(("100.20", "303.50", "150.25").Seq, ("130.44", "1505.12", "36.41").Seq, ("200.12", "305.60", "78.12").Seq).Seq`

Now, add up every first element, every second element, etc. The combination of the reduction operator and the zip meta-operators does all the work with only four characters of code: [Z+].

At this point, we have a reduced sequence:

`(430.76, 2114.22, 264.78).Seq`

The last trivial step is to print the values using the put routine. If you did the homework yesterday, you would know that say uses the gist method (which adds parentheses around a sequence) to visualise the result, while put simply prints the values using the Str method.

Let us add a few more characters to the script to demonstrate how you can skip the first column that, for example, contains month names:

`Jan 100.20  303.50 150.25Feb 130.44 1505.12  36.41Mar 200.12  305.60  78.12`

All you need is to make a slice and select all columns except the first one:

`put 'Total ', [Z+] lines.map: *.words[1..*]`

As you see, we even donâ€™t need to count columns ourselves. The 1..* range can make that job.

And thatâ€™s the end of todayâ€™s advent post, so come again tomorrow!

## 3 thoughts on “ðŸŽ„ 23/25. Calculating totals with Perl 6”

1. Elizabeth Mattijsen says:

A much more efficient way to skip the first element of a `Seq` is to use the `skip` method: `dd .skip.sum # 50`. And perhaps even better to read as well in this case. Of course, this only helps if you need to skip the first N entries of a `Seq`

Liked by 1 person