in reply to Exiting a loop trickery
The loop labels are not necessary here, but may help OMAR when the loops gain weight.LOOP_ONE: while (<>) { next unless $condition_one; last LOOP_ONE; } LOOP_TWO: while (<>) { next unless $condition_two; last LOOP_TWO; }
Be careful if reading from multiple files, as the second while will start reading from the next file if the first while exits because of EOF.
Corion pointed out that I mucked up that last paragraph.
I should have said something like this:
Be careful if reading from multiple files. You'll have to manage checking the end of each file yourself using continue. For instance, if you want to handle the first file with one while, and the second file with another, you might do something like this:which outputs something like this:while (<>) { print "$ARGV: $.: $_"; } continue { if ( eof ) { close ARGV; last; } } print "\nnext While\n\n"; while (<>) { print "$ARGV: $.: $_"; } continue { if ( eof ) { close ARGV; last; } } print "\nDone\n";Notes:X:\>while_eof file1 file2 file1: 1: file1 file1: 2: line2 file1: 3: line3 next While file2: 1: file2 file2: 2: line2 file2: 3: line3 Done
* There is a difference between eof and eof() - see perldoc -f eof.
* The line numbers in $. are reset by doing an explicit close ARGV - see perldoc perlvar.
* If using while (<>), close ARGV won't exit the while, you'll need to do a last inside the continue.
[I hope I've been sufficiently penitent :)]
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|