in reply to Re: Problem with appending files
in thread Problem with appending files

in my actual code

open (FD,">>OTAPA_Errors"); foreach my $num (@errors) { if ($flag == 1) { print FD $/,$num; } if ($flag ==0) { print FD $num; $flag = 1; } }

If I run this sometimes a new line is added at the starting. So i removed new line by using flag. But some times the new line missing.

Replies are listed 'Best First'.
Re^3: Problem with appending files
by choroba (Cardinal) on Jan 21, 2016 at 14:00 UTC
    Is the code inside a loop? Do you reset the flag for the next open of the file? If it's a global variable, you need to reset it to 0, as it keeps the old value. It's better to use strict and scope the flag properly.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^3: Problem with appending files
by hdb (Monsignor) on Jan 21, 2016 at 13:47 UTC

    May be the newline is already in the file you are appending to, only sometimes. Adding a newline accidentially is easy when inspecting a file with some editors under some OSes.

      I checked nearly 20 times that the new line is already in the file or not. But the new line is not there. I think it from input buffer. Is there any way that the input buffer can write new line into it
Re^3: Problem with appending files
by Corion (Patriarch) on Jan 21, 2016 at 13:33 UTC

    Maybe something sets or resets $/.

    Note that your code never can go into the if( $flag == 0 ) branch with the code as you show it. Maybe you want to add some debugging statements using warn. Your code can only one get into the $flag==0 branch.

    I can only recommend reducing your code so that you produce a small, self-contained program (20 lines or so) that still has the problematic behaviour.