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}\t{ '#' x $pair.value }";
}🦋 You can find the source code in the file histogram.raku.
Output
apples #####
pears ###
plums ########Comments
The string repetition operator
xbuilds each bar:'#' x 5is#####. The value simply becomes the bar length.The
\tbetween the label and the bar is a tab character, so the bars line up in a column instead of starting right after each name of a different length.