My code caputre only the first comment I am not sure why:
while (<FILE>) { ## (note: using logical indenting) if ($found) { if (/^$number/) { if (/^-/) { print COPYFILE "$_\n"; $found = 0; ## <<== this is why } } } elsif ... }
You are using a "state variable" ($found) to tell the loop whether a given line should be printed, based on whether the user-specified $name has been seen. But as soon as you print out one line, you reset $found to 0 -- only one line can be printed after $name occurs.

Actually, looking at it just now, and not knowing how you are setting $number, it's just as likely that it would never print anything at all, unless $number is always assigned some string that starts with "-" (or is never assigned any value at all, which seems more likely in this case).

My point is that you are applying two regex tests, one right after the other, that both check whether something particular occurs at the beginning of the line. That's a bit silly.

I think what you were trying to shoot for (but missed) was something like this:

$found = 0; while (<FILE>) { if ( $found ) { if ( /^\d+/ ) { # does line start with a digit? $found = 0; # then we're done printing stuff } elsif ( /^-/ ) { # otherwise, if there's stuff print COPYFILE; # then we print it } } elsif ( /^$name/ ) { $found = 1; } }
Anyway, I think some of the other proposals above are cleaner. As a closing comment, let me suggest that using consistent indentation will help.

In reply to Re: regex issue by graff
in thread regex issue 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.