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 reply to Re: perl6 phasers and a 1 liner by Anonymous Monk
in thread perl6 phasers and a 1 liner by RichardJActon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.