Advent Calendar 2025
| Day 20 | Day 21 |
The gift is presented by Feng Chang. Today he is talking about his solutioni to The Weekly Challenge - 347. This is re-produced for Advent Calendar 2025 from the original post.
PWC347, Task 1 Format Date
Following is my ch-1.raku script:
#!/bin/env raku
unit sub MAIN(Str:D $s);
my %months = <Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec> Z=> 1..12;
my ($day, $mon, $yr) = $s.words;
printf '%d-%02d-%02d', $yr, %months{$mon}, $day.substr(0,*-2);
Z=> is a more readable approach to define a hash in Raku when dealing with sequences, e.g.,
my %letters = 'a'..'z' Z=> 1..26;
put %letters.raku;
{:a(1), :b(2), :c(3), :d(4), :e(5), :f(6), :g(7), :h(8), :i(9), :j(10), :k(11), :l(12), :m(13), :n(14), :o(15), :p(16), :q(17), :r(18), :s(19), :t(20), :u(21), :v(22), :w(23), :x(24), :y(25), :z(26)}
.words is convenient in translating sentences into a list of words:
put .words is convienent in translate sentences into a list of words .words.raku;
(“.words”, “is”, “convienent”, “in”, “translate”, “sentences”, “into”, “a”, “list”, “of”, “words”).Seq
.substr(0,*-2) cuts the last 2 characters off from a string:
put 'abcdefgYZ'.substr(0,*-2);
abcdefg
If you have any suggestion then please do share with us perlweeklychallenge@yahoo.com.