push(my @file2, $_); #### my @file2; # scope it OUTSIDE of your block while () { if (s/.../.../) { push(@file2, $_); } } # end your while here?? # ... print FILE3, @file2; # prints all elements