The stupid question is the question not asked | |
PerlMonks |
Re: perl6 phasers and a 1 linerby Anonymous Monk |
on Feb 13, 2018 at 19:57 UTC ( [id://1209084]=note: print w/replies, xml ) | Need Help?? |
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!
In Section
Seekers of Perl Wisdom
|
|