Course of Raku / Addendum 🆕 / Bringing it together / Mixed mini-projects / Exercises / Scores from CSV lines
Solution: Scores from CSV lines
Here is a possible solution to the task.
Code
my @lines = 'Anna,90', 'Bob,72', 'Cara,84';
my %score;
for @lines -> $line {
my ($name, $mark) = $line.split(',');
%score{$name} = $mark.Int;
}
my $average = ([+] %score.values) / %score.elems;
say "average: $average";
say "above average:";
for %score.sort -> $pair {
say " {$pair.key}" if $pair.value > $average;
}🦋 You can find the source code in the file csv-averages.raku.
Output
average: 82
above average:
Anna
CaraComments
Splitting each line on the comma and assigning to
($name, $mark)unpacks the two fields at once;.Intturns the score text into a number.The average is the sum of the values over their count; anyone whose score beats it is listed. Here the average works out to a whole
82.