in reply to Problem with appending files

What is the output when it is working? Have you made sure that the problem is related to appending to a file? Does your script output the expected output when it is writing to STDOUT instead of appending to a file?

Replies are listed 'Best First'.
Re^2: Problem with appending files
by ravi45722 (Pilgrim) on Jan 21, 2016 at 13:31 UTC

    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.

      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,

      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

      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.