Count the number of words in a text.
Before solving the task, let us assume that by words we mean here a sequence of alphanumeric characters, including the underscore symbol.
Here is the solution:
my $text = prompt('Text> ');
say $text.comb(/\w+/).elems;
Try it on a few test inputs:
$perl6 countwords.pl
Text> Hello, World;
2
The program uses regexes for extracting words using the \w
character class and the combstring method that returns a sequence of the words:
$text.comb(/\w+/)
The +
quantifier allows a repetition of \w
, so it matches the whole word.Â
Alternatively, a more traditional match with a regex may be used:
$text ~~ m:g/(\w+)/;
say $/.elems;
Parentheses in the regex capture the word, and the :g
adverb applies it a few times until all the words are found. The $/
variable (called the match object) keeps all the matched substrings, and the elems
method returns the number of elements in it.