open (MYOUTFILE, ">outfile.txt") or die "Can't create outfile: $!"; # Other code print MYOUTFILE "$A,$B,n/a\n" if $i==0; print STDOUT "$A,$B,n/a\n" if $i==0; # Other code close MYOUTFILE; #### if ($i==0){ print MYOUTFILE "$A,$B,n/a\n"; print STDOUT "$A,$B,n/a\n"; }