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

Re: Raku -npe command line usage

Thread Previous | Thread Next
May 8, 2020 15:49
Re: Raku -npe command line usage
Message ID:
All good ideas so far, in the "more than one way to do it" spirit, can use
"state" instead of "my", since state only initializes 1st time it's hit.

    raku -ne 'state @i;@i.push($_) if .starts-with(q[WARN]); END .say for
@i.sort' sample.log

Or adapting Brad's answer with the feed operator for fun

    raku -e 'for lines() ==> grep /^WARN/ ==> sort() {.say}' sample.log

Now, I didn't want to use 'map' in there, because of a habit of only using
'map' when I want the return values. When looping for side-effects only,
like saying each value in a list, I want to use 'for'. UnFORtunately though
I cannot find anything as clean looking as

    raku -e 'lines() ==> grep /^WARN/ ==> sort() ==> map *.say' sample.log

reading entirely L-to-R which does NOT use map... ideas?


On Fri, May 8, 2020 at 10:10 AM William Michels via perl6-users <> wrote:
> On Fri, May 8, 2020 at 5:16 AM WFB <> wrote:
> >
> > Hi,
> >
> > I am trying to write an one-liner to go through all lines in a logfile
and look for an certain key word, store the line and sort them before
printing them out.
> >
> > My approach was:
> > raku -ne "BEGIN {my @i }; @i.push($_); if $_ ~~ /^WARN/; END {
@i.sort.say }"
> > That does not work because @i does not exist in the if clause. I tried
our @i as well with no luck.
> >
> > How can I store data that can be accessed in the END phaser? Or is
there another way to archive it? TIMTOWTDI^^
> >
> > One hint I found was the variable $ and @ respectively. But those
variables are created for each line new...
> >
> >
> > I did not found a help or examples for -npe except raku -h. Is there
more helpful stuff somewhere in If so I could'nt find it.
> >
> > Thanks,
> > Wolfgang
> Hi Wolfgang,
> This is a first attempt at doing what you want: I'm sure it can be
> shortened. Since one of your requirements is doing a sort on filtered
> values stored in an array, I abandoned use of the "-ne" one-liner
> flag, using "-e"  and "for lines()" instead. I also used grep instead
> of smart-matching:
> perl6 -e 'my @i; for lines() {if .grep(/^WARN/) -> ($s)
> {@i.push($s)};}; .say for @i.sort;'
> Note: the "-> ($s)" section where I store grepped matches comes from a
> Jonathan Worthington answer found here (thanks Jonathan!):
> I certainly would be interested to learn if there's a phaser solution
> to this problem (and I also have a sneaking suspicion that Supply
> might be useful  here... ).
> HTH, Bill.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About