Course of Raku / Objects, I/O, and exceptions / Input and output / File handles / Exercises / Count the lines

Solution: Count the lines

Here is a possible solution to the task.

Code

spurt 'data.txt', "a\nb\nc\nd\n";

say 'data.txt'.IO.lines.elems;

🦋 You can find the source code in the file count-lines.raku.

Output

4

Comments

  1. .IO.lines returns the lines of the file as a list.

  2. .elems counts them, giving 4.

Course navigation

Count the lines   |   Write with a handle