in reply to Re: advancing in file read
in thread advancing in file read

How is that different than what he is doing now (reading the entire file into a list, then looping through the list)? Your solution is incomplete.

Replies are listed 'Best First'.
Re^3: advancing in file read
by apl (Monsignor) on May 16, 2010 at 21:13 UTC
    You are correct; thanks.
    1. Read the file into an array without processing the content.
    2. Set an index to 0
    3. Process the contents of the current index of the array. If said contents require information on the next line, refer to the (index + 1) element of the array.
    4. Increment the index by 1 or 2, as appropriate.
      So
      my @array = <$fh>; for (my $i=0; $i<@array; ++$i) { my $line = $array[$i]; ... if (...) { defined( my $next_line = $array[++$i] ) or die; ... } ... }

      It's simpler just doing

      while (my $line = <$fh>) { ... if (...) { defined( my $next_line = <$fh> ) or die; ... } ... }
        It is if the same processing must be applied to every line in the file, even if said line was the second line of an earlier piece of processing.

        Requiring the file be read at the top of the loop denies you the ability to reprocess the second line. The OP had said

        And is there a way to advance the file handle so it doesn't re-read the next line in the foreach loop?