in reply to How do I backtrack while reading a file line-by-line?

Every time to you come to a line that starts with '>', you could push it onto an array. Then refer back to the array each time you want to access previous lines that started with '>'. I don't know how many times you come across lines that (since it can create quite a large array).

That being said, to add onto ikegami's idea, you can use tell to tell you where the line is, push that on an array. Then when you want to go back X number of times, then you can always seek to the line ($lines[-1] .. $lines[-4]).

Replies are listed 'Best First'.
Re^2: How do I backtrack while reading a file line-by-line?
by Fletch (Bishop) on Oct 13, 2006 at 20:46 UTC

    Keeping your own buffer also has the advantage of working on something that's not seekable (e.g. a network socket, or a pipe from another program).

Re^2: How do I backtrack while reading a file line-by-line?
by mdunnbass (Monk) on Oct 18, 2006 at 20:14 UTC
    Unfortunately, I am reading in files that contain genome data, at the lines starting with '>' correspond to the start of a new chromosome. So, a ~500 Mb file will contain less than 50 lines starting with '>'. So, reading everything inbetween them into the buffer almost defeats the purpose of the buffer itself.

    Thanks anyway tho.
    Matt