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
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.while ( $line = <FH> ) { # do stuff to $line }
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).
Thanks again- blyman#!/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
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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |