Course of Raku / Objects, I/O, and exceptions / Input and output / Working with files / Exercises / Count the characters

Solution: Count the characters

Here is a possible solution to the task.

Code

spurt 'data.txt', 'Raku';

say slurp('data.txt').chars;

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

Output

4

Comments

  1. spurt writes the string Raku to the file.

  2. slurp reads it back as a string, and .chars counts its characters — four.

  3. If you inspect the file, you will indeed only see the four characters; the newline character is not added.

Course navigation

Count the characters   |   Working with directories