How to loop on sorted (with custom sort) keys of a hash in Raku?

10

Trying to progressively convert some Perl scripts to Raku. I am quite stuck with the following one, even after browsing quite a lot here and reading Learning Perl 6 more deeply.

The part on which I can't make progress is the last loop (converted to for); getting keys and sorting them by month name and day number looks impossible, but I am sure it is doable.

Any hints on how to achieve this with "idiomatic" syntax would be really welcome.

#!/usr/bin/perl

use strict;

my %totals;

while (<>) {
    if (/redis/ and /Partial/) {
        my($f1, $f2) = split(' ');
        my $w = $f1 . ' ' . $f2;
        $totals{$w}++;
    }
}

my %m = ("jan" => 1, "feb" => 2, "mar" => 3, "apr" => 4, "may" => 5, "jun" => 6,
         "jul" => 7, "aug" => 8, "sep" => 9, "oct" => 10, "nov" => 11, "dec" => 12);

foreach my $e (sort { my($a1, $a2) = split(' ', $a) ; my($b1, $b2) = split(' ', $b) ;
            $m{lc $a1} <=> $m{lc $b1} or $a2 <=> $b2 } keys %totals) {
    print "$e", " ", $totals{$e}, "\n";
}
Share
Improve this question
New contributor
user15795022 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • 1
    I haven't read beyond your question title yet because I check for comments/answers first and want to reply to your comment first. I've never heard of anyone not being able to edit their Qs. A quick google turned up this. It only talks about account snafus. Then again it's generic to all stackexchange sites, not specifically stackoverflow, and individual stackexchange sites are allowed to have their own rules. Maybe search of meta.stackoverflow.com? I'll now turn my attention to your question. Oh, and Hi! :) – raiph Apr 29 at 19:42
  • 2
    Oh, one other thing. The broader stackoverflow culture has historically been heavily biased toward dissuading friendly chit chat. While I feel we do need to be respectful of our hosts, Rakoons nevertheless generally (quietly, shhh) say "pfft" to such "we're all too busy to be human" stuff -- our historical norm is to encourage -Ofun, and part of that is encouraging things like friendliness and jokes. You have been warned... – raiph Apr 29 at 19:49
  • 1
    Oh! You've posted your Perl code, not your Raku code. Fortunately I know a bit of Perl so fair enough, but... Oh! Now @wamba has posted an answer. Here's me too busy chatting... :) – raiph Apr 29 at 19:52

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue