Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: perl6 phasers and a 1 liner

by RichardJActon (Novice)
on Feb 14, 2018 at 14:36 UTC ( [id://1209142]=note: print w/replies, xml ) Need Help??


in reply to perl6 phasers and a 1 liner

Thank You Monks,

I setteled on:

 perl6 -ne 'my $ids; BEGIN {$ids="patternFile.txt".IO.lines.Set}; my @F=$_.split("\t"); if @F[0] (elem) $ids {@F.join("\t").say}' searchFile.txt

Much as Anonymous Monk suggested but with Laurent_R's use of declaring $ids outside of the BEGIN block / before the BEGIN phaser. I think that Anonymous Monk's original suggestion would have re-read the patternFile on every line which would be inefficient.

In retrospect I clearly had an equivocation in my mental model of what lines does a I used it to pull in all the lines for the first file and then tried to used it a bit like <> from perl5 to read in 1 line at a time from STDIN.

Thanks again!

What is true is already so. Owning up to it doesn't make it worse. - Eugene Gendlin

Replies are listed 'Best First'.
Re^2: perl6 phasers and a 1 liner
by Laurent_R (Canon) on Feb 15, 2018 at 19:07 UTC
    Since this is a one-liner, you might want to shorten it a bit. Please note that a method call without invocant will be invoked with $_.

    So you could change:

    my @F=$_.split("\t");
    to
    my @F= .split("\t");
    Similarly you appear to be printing the whole line. So, rather than:
    {@F.join("\t").say}
    you could have:
    {.say}
    I think that Anonymous Monk's original suggestion would have re-read the patternFile on every line which would be inefficient.
    Probably not. A state variable is initialized only once. From the documentation (https://docs.perl6.org/syntax/state):
    However, initialization happens exactly once the first time the initialization is encountered in the normal flow of execution. Thus, state variables will retain their value across multiple executions of the enclosing block or routine.
    But this should probably be confirmed with a test.

    Update: added the word "variable" which was missing in one sentence ("A state variable ...")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209142]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found