๐Ÿ“˜ Count vowels in a word using Perl 6

๐Ÿ“˜ Count vowels in a word using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


Count the number of vowel letters in the given word.

Of course, we will abstract now from the difference between letters and sounds and will only count the number of vowel letters: a, e, i, o, and u.

my $word = 'Hello';
$word ~~ m:g:i/<[aeiou]>/;
say $/.elems;

Here, the given $word is matched against a regex that contains a character class with all the letters of our interest. Character classes in regexes use the <[...]> syntax. The regex is prefixed with the :g adverb, making it match as many times as needed to cover the whole string. The second adverb, :i, makes the regex case-insensitive.

The result of the match is a match object $/, which contains all the occurrences of vowel letters found in the word. Its length is the number we need. So, printing $/.elems gives the answer.

To extend the match to allow characters with diacritics, such as รผ, the simplest is to add another adverb, :m, as shown in the next example.

$word = 'Mรผnich';
$word ~~ m:g:i:m/<[aeiou]>/;
say $/.elems;

This program prints 2, as both รผ and i matched.

If you prefer the long adverb names, here is the code:

$word ~~ m:global:ignorecase:ignoremark/<[aeiou]>/;

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code