Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: perl6 phasers and a 1 liner

by Anonymous Monk
on Feb 13, 2018 at 19:57 UTC ( #1209084=note: print w/replies, xml ) Need Help??


in reply to perl6 phasers and a 1 liner

the thing you're missing here is that my variables are limited in scope to the curlies that contain them. You can, however, use phasers without curlies, too. that would solve your problem nicely, i expect. other than that, you can have @ids be a state instead of my variable, which lets you get "initialize only once" semantics without using a phaser.

Another problem is that you have a -ne (i.e. run once for every line) but you use the lines sub in your code, which will immediately give you all lines as one list. Then the split method on that will turn it into a string for you (by concatenating all the lines) and then you split by "\t". That means that @F[0] will only contain the very first field of the very first line, and @F.join("\t").say will output the whole file (if the first field of the first line is contained in the patternFile.txt). What you probably wanted was to have my @F = $_.split("\t").

Another thing is that you really want to compute @ids.Set once rather than for every single line. For a Set, however, you have to use either a %-sigiled variable and bind (:=) the Set (because assignment will turn the right-hand side into a Hash for you) or a $-sigiled variable and assign the Set. Binding doesn't work with state variables, though, so you're left with this:

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

Hope that helps!

Replies are listed 'Best First'.
Re^2: perl6 phasers and a 1 liner
by Laurent_R (Canon) on Feb 13, 2018 at 22:39 UTC
    Another problem is that you have a -ne (i.e. run once for every line) but you use the lines sub in your code, which will immediately give you all lines as one list.
    You seem to have missed that the code is using two different files. The OP uses the .lines IO method to read patternFile.txt in the BEGIN block, and the -ne command line option to read searchFile.txt.
      i was actually referring to the lines sub, not the lines method. both occur in the code, and the part that uses the sub is the one that is the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1209084]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-06-22 21:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (124 votes). Check out past polls.

    Notices?