TIMTOWTDI:
LOOP_ONE: while (<>) { next unless $condition_one; last LOOP_ONE; } LOOP_TWO: while (<>) { next unless $condition_two; last LOOP_TWO; }
The loop labels are not necessary here, but may help OMAR when the loops gain weight.

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:
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";
which outputs something like this:
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
Notes:
* 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


In reply to Re: Exiting a loop trickery by QM
in thread Exiting a loop trickery by GaijinPunch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.