Course of Raku / Addendum 🆕 / Working with data / Text and strings / Exercises / Run-length encoding

Solution: Run-length encoding

Here is a possible solution to the task.

Code

my $text = 'aaabbbbcc';

my $encoded = '';
for $text ~~ m:g/ (.) $0* / -> $match {
    $encoded ~= $match[0] ~ $match.chars;
}

say $encoded;

🦋 You can find the source code in the file run-length.raku.

Output

a3b4c2

Comments

  1. The pattern (.) $0* captures one character and then matches as many further copies of it as follow — one whole run per match. m:g collects every run.

  2. For each run, $match[0] is the repeated letter and $match.chars is how long the run is, so the two together give entries like a3.

  3. Naming the match with -> $match is optional. Without it, each match becomes the topic $_, and you can drop the variable and call the methods on it directly:

    my $text = 'aaabbbbcc';
    my $encoded = '';
    
    for $text ~~ m:g/ (.) $0* / {
        $encoded ~= .[0] ~ .chars;
    }
    
    say $encoded;

Course navigation

Run-length encoding   |   Lists, arrays, and hashes