On Jun 30, David Arnold said:

>As I begin reading in lines from the file, I just print them until I hit a
>line that has an opening "\ex" in it. At that point I want to accumulate
>lines in one long string until I hit either "\begin{instructions}" or
>another "\ex".
>
>$line.=<IN>   #unless the current line coming in from IN is the start
>                 #of a new \ex or a \begin{instructions}
>
>The difficulty is now I've read one line too many. I'd like to "put this
>last line back" for the next round of reading while I process the
>accumulated exercise lines.

I would suggest the following approach:

  # some bigger loop
  while (...) {
    my $line = "";

    while (<IN>) {
      if (/\\ex|\\begin{instructions}/) {
        seek IN, -length, 1;
        last;
      }
      $line .= $_;
    }
  }

This uses the seek() function to go to a position in the file.  The last
argument, 1, means we're moving relative to where we are now.  The middle
argument, -length, is the number of bytes to move.  So if the line is 20
characters long, we're going 20 characters back from where we are now,
essentially to the start of the line.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
CPAN ID: PINYAN    [Need a programmer?  If you like my work, let me know.]
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to