Course of Raku / Addendum 🆕 / Bringing it together / Mixed mini-projects / Exercises / A text histogram

Solution: A text histogram

Here is a possible solution to the task.

Code

my %sales = apples => 5, pears => 3, plums => 8;

for %sales.sort -> $pair {
    say "{$pair.key}: { '#' x $pair.value }";
}

🦋 You can find the source code in the file histogram.raku.

Output

apples: #####
pears: ###
plums: ########

Comments

  1. The string repetition operator x builds each bar: '#' x 5 is #####. The value simply becomes the bar length.

Course navigation

A text histogram   |   Group the anagrams