sub char_count($str) {
my %res;
my @chars = $str.comb;
for @chars -> $c {
%res{$c}++;
}
%res;
}
my $str = 'Article 1. All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.';
my %char_count = char_count($str);
my @char_count_sorted = %char_count
.pairs
.sort(sub ($a, $b) {
$cmp = $a cmp $b;
if ($cmp === Order::More) {
Order::Less;
} elsif ($cmp === Order::Less) {
Order::More;
} else {
$cmp
}
});
say "String: $str";
say '';
say 'Character occurence (most frequent to least frequent):';
for @char_count_sorted -> $pair {
my $char = $pair.key;
my $count = $pair.value;
my $display_char;
if ($char eq ' ') {
$display_char = '<space>';
} else {
$display_char = $char;
}
say "$display_char: $count";
}