You've got two nested while loops, both reading from the same filehandle. That may not be what you want.

Here's what'll happen: the outer loop will read lines from the file. As soon as a match is found (if ($line1 =~ /$pattern/)), the second, inner loop will start and continue reading from where the first loop left off. When it's done, the filehandle is exhausted, and the outer loop, noticing that there's no data left to read from the file, will terminate.

Note that

  1. the inner loop will not start reading from the start of the file, and
  2. the outer loop will not pick up where you left off before the inner loop started again.

Either of these may well be intended, expected behavior, but I'm mentioning it just in case.

If it's not intended/expected, you can use tell and seek to save the filehandle's current position and restore it, though there'll likely be easier, more elegant ways of achieving what you want to achieve.


In reply to Re: pattern and loop by AppleFritter
in thread pattern and loop by Anonymous Monk

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.