develooper Front page | perl.perl6.users | Postings from May 2020

Re: Raku -npe command line usage

Thread Previous
From:
yary
Date:
May 8, 2020 17:13
Subject:
Re: Raku -npe command line usage
Message ID:
CAG2CFAbpJbvwtyngF06hTzpqC0FM4JoHjbEjA8YNyZxNabYi+A@mail.gmail.com
Ooops forgot the sort... let's golf with the hyper-operator again...

    raku -ne'push my @i: $_ if .starts-with: q[WARN]; END @i.sort>>.say'
sample.log

-y


On Fri, May 8, 2020 at 1:10 PM yary <not.com@gmail.com> wrote:

> ooh neat! I didn't know. Indeed this works. Thanks Sean!
>
>     raku -ne'push my @i: $_ if .starts-with: q[WARN]; END .say for @i'
> sample.log
>
> -y
>
>
> On Fri, May 8, 2020 at 1:04 PM Sean McAfee <eefacm@gmail.com> wrote:
>
>> On Fri, May 8, 2020 at 6:53 AM Brad Gilbert <b2gills@gmail.com> wrote:
>>
>>> So together that would be:
>>>
>>>     raku -ne 'BEGIN my @i; @i.push($_) if /^WARN/; END .say for @i.sort'
>>>
>>
>> Or alternately the main body of the loop can be written:
>>
>>     (my @i).push($_) if /^WARN/;
>>
>> Or even:
>>
>>     push my @i: $_ if /^WARN/;
>>
>> It's so nice how Raku essentially compiles the body of these file loops
>> into a little subroutine so that the "my" declaration only occurs once,
>> unlike how Perl 5 just textually wraps the loop with "while (<>) {" and "}"
>> which makes the declaration occur on every iteration.
>>
>> I originally figured this out when I idly worked up a classic
>> word-frequency-count one-liner:
>>
>>     raku -ne '++(my %freq){$_} for m:g/\w+/; END .say for
>> %freq.antipairs.sort.reverse' file ...
>>
>>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About