Course of Raku / Essentials / Positional data types / Exercises / Count and print command-line arguments
Solution: Count and print command-line arguments
In this program, a for
loop is a good choice.
Code
for ^@*ARGS -> $n {
say "{$n + 1}. @*ARGS[$n]";
}
🦋 Find the program in the file count-and-print-arguments.raku.
Output
Run the program and confirm it prints the arguments and line numbers:
$ raku exercises/positionals/count-and-print-arguments.raku one two three four
1. one
2. two
3. three
4. four
Discussion
This program uses a few handy tricks. First, the for
loop goes along the range constructed with ^
. So, the range starts from 0 and goes up to (but not including) the value of @*ARGS
. In this context, it returns the lengths of the array.
As the first element of an array has index 0
, and the task demands we count the lines from 1
, this simple shift is computed inside a code block in a string: "{$n + 1} ..."
.
Next exercise
Course navigation
← Typed variables / Allomorphs | Associative data types →
Translations of this page: English • Deutsch • Español • Italiano • Latviešu • Nederlands • Български • Русский • Українська