Course of Raku / Addendum 🆕 / Working with data / Lists, arrays, and hashes / Exercises / Zip two lists into a hash
Solution: Zip two lists into a hash
Here is a possible solution to the task.
Code
my @names = <Anna Bob Cara>;
my @scores = 90, 85, 95;
my %result = @names Z=> @scores;
for %result.sort -> $pair {
say "{$pair.key}: {$pair.value}";
}🦋 You can find the source code in the file zip-to-hash.raku.
Output
Anna: 90
Bob: 85
Cara: 95Comments
Z=>is the zip metaoperator applied to the pair constructor=>: it walks both lists in step and builds aname => scorepair from each position.Assigning that list of pairs to a
%variable makes a hash, which is then printed in key order.