Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: advancing in file read

by apl (Monsignor)
on May 16, 2010 at 21:13 UTC ( #840262=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: advancing in file read
by ikegami (Pope) on May 16, 2010 at 21:26 UTC
    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?

        It's my understanding that the OP doesn't want to re-read the line because he doesn't want to reprocess it, not for efficiency purposes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840262]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-09-25 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (276 votes). Check out past polls.

    Notices?