The 「@i」 is defined within the 「BEGIN」 block, so it is scoped to the
「BEGIN」 block.

If you didn't want that, don't use a *block* with 「BEGIN」.

    BEGIN my @i;

Also you wrote the 「if」 wrong.
There shouldn't be a 「;」 before the 「if」.
You also don't need to use 「$_ ~~ 」 with 「/^WARN/」 as that would
automatically happen.

    @i.push($_) if /^WARN/;

You probably also want the lines to be written each to to their own lines.
So you probably want one of the following.

    @i.sort.map( *.say )
    .say for @i.sort

Again you don't need to use a block with 「END」 either.

So together that would be:

    raku -ne 'BEGIN my @i; @i.push($_) if /^WARN/; END .say for @i.sort'

---

That said, I wouldn't use -n in this case.

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

Currently regexes are a bit slow. You can use 「.starts-with」 instead to
make it faster.

    raku -e 'lines.grep( *.starts-with(q[WARN]) ).sort.map( *.say )'

(Note that 「 'abc' 」 is short for 「 q'abc' 」 which can also be written as 「
q[abc] 」.
The latter is useful on the command line so that it doesn't interfere with
the command processor quoting rules.)

On Fri, May 8, 2020 at 7:16 AM WFB <wolfgang.banas...@gmail.com> 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 doc.raku.org? If so I could'nt find it.
>
> Thanks,
> Wolfgang
>

Reply via email to