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.while (<FILE>) { ## (note: using logical indenting) if ($found) { if (/^$number/) { if (/^-/) { print COPYFILE "$_\n"; $found = 0; ## <<== this is why } } } elsif ... }
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:
Anyway, I think some of the other proposals above are cleaner. As a closing comment, let me suggest that using consistent indentation will help.$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; } }
In reply to Re: regex issue
by graff
in thread regex issue
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |