If you're not married to the "key : value" format, you could use this:

    scan +spam | perl6 -ne 'my %d; %d{.words[1]}++;  END { .say for sort %d

Here's another variation, but keeping your original format:

    scan +spam | perl6 -ne 'my %d; %d{.words[1]}++;  END { say "$_.key() :
$_.value()" for sort %d }'

And that *really* made me wish that this one would work:

    scan +spam | perl6 -ne 'my %d; %d{.words[1]}++;  END { for %d.kv.sort
-> $k, $v { say "$k : $v" } }'  # oops

But the .kv method gives a list of key,value,key,value,etc. which doesn't
quite sort the way you want.
However, this one does work:

    scan +spam | perl6 -ne 'my %d; %d{.words[1]}++;  END { for
%(%d.sort).kv -> $k, $v { say "$k : $v" } }'

At the expensive of some extra punctuation and a little bit of clarity.
Personally, I'd just use the first thing I said.

With strict being on as the default, It's too bad that one-liners aren't
treated as routines such that you can use self-declared parameters like so:

    scan +spam | perl6 -ne '%^d{.words[1]}++;  END { .say for sort %d }'

Then we could get both stricture and ease-of-use. Though, they'd have to be
auto-declared read-write as well for this to work.  And though they'd be
parameters to this routine, the routine would probably be required to be
called with no parameters.  And that would also mess with the call to
.words as $_ wouldn't be right ... but ... details, details  ;)

There may be the kernel of a good idea there if someone smarter than me
wants to think about it some more and make it workable.


On Tue, Sep 1, 2015 at 2:10 AM, Matija Papec <mpapec2...@yandex.com> wrote:

> Not pretty, also you'll have to take care of -a switch,
> perl6 -ne 'our %d; %d{ .trim.split(/\s+/)[1] }++; END {say "$_: %d{$_}"
> for sort keys %d}'
> 31.08.2015, 17:25, "yary" <not....@gmail.com>:
> > Once in a while, our sysadmin tweaks something on an upstream mail
> server, and asks us a few days later if our spam rate has changed. I
> invariably whip up a perl5 one liner like this to get a daily spam count
> from my "mh" mail folder:
> >
> > scan +spam|perl -naE '$d{$F[1]}++; END{say "$_: $d{$_}" for sort keys
> %d}'
> >
> > "scan +spam" spits out one line per message in my spam folder, with the
> date in field 1. I'm using perl in an awk-like way, and taking advantage of
> non-strictness to use a global %d without declaring it.
> >
> > What's a good concise "strict" Rakudo one-liner that works the same? In
> particular, is there a Perl6 one-liner that works line-by-line (not
> requiring reading all STDIN into memory) and doesn't require a variable
> declaration?
> >
> > -y

Reply via email to