Sorry,
while( ${ln} <= $#lines )
That $# starting with -1 thing really gets to me.
On Wed, 30 Jun 2004 16:38:45 -0400, perl.org wrote
> If the file is relatively small, why not read it into an array, then
> just manipulate the array index? Something like:
>
> my @{lines} = <IN>;
> close( IN );
>
> my ${ln} = 0;
> while( ${ln} <= $#lines + 1 )
> {
> # check ${lines[${ln}]} and manipulate ${ln} accordingly.
> }
>
> > I would suggest the following approach:
> >
> > # some bigger loop
> > while (...) {
> > my $line = "";
> >
> > while (<IN>) {
> > if (/\\ex|\\begin{instructions}/) {
> > seek IN, -length, 1;
> > last;
> > }
> > $line .= $_;
> > }
> > }
> >
>
> On Wed, 30 Jun 2004 16:32:00 -0400 (EDT), Jeff 'japhy' Pinyan wrote
> > 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.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>