Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://840262]
[nysus]: What happened to the submit button? All I have is a preview button.
[erix]: perlmonks has become read-only ?
[marto]: erix unlikely, we've had posts within the last few minutes ;)
[ambrus]: nysus: make sure you enter at least two lines for the title and then preview, and read warnings the preview form prints
[davies]: There is an option (I have it set) to force preview before submission. Perhaps your option has been set (accidentally?) and you are not expecting it.
[ambrus]: sorry, I mean at least two words for the title and then preview
[ambrus]: davies: that option is the default. and it's not really "force", it just hides the button.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2017-12-15 11:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.