Thanks to all who responded. I should have mentioned earlier that my file is 5meg in size with 169312 lines - larger than I want to slurp into an array all at once.

It's been a bit of a trick to go through and figure out what the different replies have in common. The most obvious trick (to others, at least!) was $foo = <FH>; which apparently pops a single line off of FH, sticks it in $foo, and updates FH so the next read will return the next line in the file.

I feel pretty silly; I'm used to code like this

while ( $line = <FH> ) { # do stuff to $line }
but I didn't realize I could $foo = <FH>; outside of a while-ing context. While this is a worthwhile thing to know, for me the larger lesson was using a different approach.

tadman said it best: you could just re-work how you're using Perl. Indeed. I was thinking I needed to read three lines, process them, then skip backwards two lines and repeat the cycle. It seems that everyone else realized I just needed to prime my array by reading two lines, then push a third line onto the array, process the lot, shift the first one off, and start pushing and processing again.

For me the neat part about SOPW and the various answers is finding out which answer (if any) the poster decided to go with. In my case, I'm going with the answer that looks the most like my current code - an adaptation of tadman's response. This isn't based off of benchmarks or how easily I can incorporate the changes into my existing code. Perhaps it just seems closest to my Perl idiolect (awkward pidgin that it is).

#!/usr/bin/perl use strict; use warnings; my @three; # Stoke it chomp ($three[0] = <DATA>); chomp ($three[1] = <DATA>); while(<DATA>) { chomp ($three[2] = $_); # Do stuff to process @three print join ("\t", @three), "\n"; # Rotate shift(@three); } __DATA__ 1 2 3 4 5 6
Thanks again- blyman
setenv EXINIT 'set noai ts=2'

In reply to Re: Grabbing lines three-by-three from a file by belden
in thread Grabbing lines three-by-three from a file by belden

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.