ramesh_nathan has asked for the wisdom of the Perl Monks concerning the following question:

hi guys

I have a question on reading files in PERL.

I have adopted the general approach of putting the file handle inside a while loop and then reading it line by line. Now if I need to go back to a particular line say line number 30 ...how do I do it if I have already passed this line. Would really appreciate if anybody can help me..

emailramesh_nathan@yahoo.com

Replies are listed 'Best First'.
Re: FILE operation in PERL
by BrowserUk (Patriarch) on Jul 19, 2003 at 07:10 UTC

    The very easiest way of doing this is to use Tie::File. It allows you to treat the whole files as an array of lines without requiring the whole file to be in memory.

    It effectively does the telling and seeking for you and adds in intelligent buffering to boot.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: FILE operation in PERL
by dws (Chancellor) on Jul 19, 2003 at 06:19 UTC
    Now if I need to go back to a particular line say line number 30 ...how do I do it if I have already passed this line.

    There are two approaches.

    1. Read the entire file into memory. (E.g., as an array of lines.) You get rapid access to arbitrary lines.

      my @lines = <FILE>; will read from an open file handle into an array.

    2. If the file can't fit in memory (or you choose not to read it all in at once), use tell() to remember interesting places, and seek() to get you back there. They're covered in perlfunc.

    Update: tilly pointed out that it's also possible to re-read the file. You can do that by closing and reopening the file (which assumes that the file hasn't been rewritten in the interim), or you can rewind the file handle.

      Thanks for your tips...It looks like now I am really close to solving the problem...Just one glitch... I have used the "tell" function to store the location of interest...and then I use the "seek" function to take me there...When I then try to read using $line=<FILE>, I am able to read the next line following the line I actually want....Can u please provide any input on this...
        When I then try to read using $line=<FILE>, I am able to read the next line following the line I actually want...

        Do the tell() before you read the line. Otherwise, as you've noticed, you're one line too late.

Re: FILE operation in PERL
by sgifford (Prior) on Jul 19, 2003 at 06:11 UTC
    You have two choices. Either you can save the whole file in an array, with one line per array element, in which case you can just refer to $array[30], or else on the way by you can use tell to find out where you are right before you read a line, then use seek to go back there.

      Hers' a sample implementation

      my @line_offsets; push @line_offsets, tell *FOO; while (my $line = <FOO>) { push @line_offsets, tell *FOO; if ( foobar() ) { seek *FOO, $line_offsets[29]; } }
A reply falls below the community's threshold of quality. You may see it by logging in.