xerox@eleazar.dartmouth.edu (Jamie Osborne) (05/29/91)
Is there any easy way to peek ahead using the while(<>) { construct? I want to be able to read the next line, but I don't want to advance <> yet. Help anyone? Please email me: xerox@park.dartmouth.edu -- James W. Osborne | email: xerox@mac.dartmouth.edu Systems Programmer and starving student | Disclaimer:I didn't mean it! Really! "The best thing you ever done for me is to help me take my life less seriously. It's only life, after all." -Indigo Girls
inc@tc.fluke.COM (Gary Benson) (05/29/91)
In article <1991May29.004259.11825@dartvax.dartmouth.edu> xerox@eleazar.dartmouth.edu (Jamie Osborne) writes: > >Is there any easy way to peek ahead using the while(<>) { >construct? I want to be able to read the next line, but I don't want >to advance <> yet. > >Help anyone? I do this, but I'm afraid that there isn't a way to avoid advancing <>. Instead, you trick it. For each line that you want to peek, put it into an array and keep track of the number of lines going in. Now, when you want to advance <>, look at the "lines" array first. If there's something in there, use it instead of <>; if there's nothing there, use the real <>. Here's my "peek" subroutine: #****************************************************************************** # PEEK Peek N lines ahead in the input stream. # sub peek { $tmp = $_; $i = $#lines+1; while ($i<$_[0] && !eof()) { $i++; $_ = <>; &prefilter(); push(@lines,$_); } $_ = $tmp; &prefilter(); if ($#lines >= $_[0]-1) { $lines[$_[0]-1]; } } #****************************************************************************** That's PART ONE. The second part involves how you send things to the output. Here's my "printline" subroutine: #****************************************************************************** # PRINT LINE Substitute special characters and then print the line. sub printline { # first, find the line to print, either passed in or default $tmp = $#_ < 0 ? $_ : $_[0]; &spec_char_subs($tmp); print OUTPUT $tmp; } #****************************************************************************** Hope that helps. -- Gary Benson -=[ S M I L E R ]=- -_-_-_-inc@fluke.com_-_-_-_-_-_-_-_-_-_- Selling software is like prostitution; you've got it, you sell it, you've still got it! -D. Lambert (IST)
jbryans@beach.csulb.edu (Jack Bryans) (05/30/91)
In article <1991May29.004259.11825@dartvax.dartmouth.edu> xerox@eleazar.dartmouth.edu (Jamie Osborne) writes: > Is there any easy way to peek ahead using the while(<>) { > construct? I want to be able to read the next line, but I don't want > to advance <> yet. Try: loop: while(<>) { . . . if ( next-line-peek-stuff ) { whatchya-wanna-do-if next; } redo loop; } The "redo"'ll start you over w/the "peek'ed at" line as the "next" line. Jack