while () { ## (note: using logical indenting) if ($found) { if (/^$number/) { if (/^-/) { print COPYFILE "$_\n"; $found = 0; ## <<== this is why } } } elsif ... }