he204035 has asked for the wisdom of the Perl Monks concerning the following question:

I have some code to open a text file, and based on what is found in the text I am analyzing, place the line of text in a different file.

I am opening the receiving files as "append", but the files keep getting written over, not appended:

foreach $subdir(@subdirs) { chdir($subdir); @files=(); @files = glob "*.txt"; foreach $file(@files) { open(MYFILE, "<", $file); open($ohfivei, '>>905i.txt'); open($ohthreei, '>>903i.txt'); open($teni, '>>910i.txt'); open($ohfiveo, '>>905o.txt'); open($ohthreeo, '>>903o.txt'); open($teno, '>>910o.txt'); while ($line=<MYFILE>) { #print "$_\n"; if($line=~(/$subdir/)) { ($undef, $mon, $day, $yr, $hr, $undef, $undef, $pr, $dir +, $cc, $undef, $undef, $pegs, $undef) = split /,/, $line; $dt = ("$mon-$day-$yr $hr:00"); if ($pr=="905") { if($dir=="1") { print $ohfivei "$dt,$cc,$pegs\n"; } elsif($dir=="2") { print $ohfiveo "$dt,$cc,$pegs\n"; } } elsif ($pr=="903") { if($dir=="1") { print $ohthreei "$dt,$cc,$pegs\n"; } elsif($dir=="2") { print $ohthreeo "$dt,$cc,$pegs\n"; } } elsif ($pr=="910") { if($dir=="1") { print $teni "$dt,$cc,$pegs\n"; } elsif($dir=="2") { print $teno "$dt,$cc,$pegs\n"; } } } } close MYFILE; close $ohfivei; close $ohthreei; close $teni; close $ohfiveo; close $ohthreeo; close $teno; unlink "$file";

I have tried using an ALLCAPS filehandle instead of "$fh" format, but I get the same thing. I have also tried double-quoting the ">>", with the same results.

.

I think I may be to the point of "can't see the forest through the trees", so I thought I would ask for help.

Replies are listed 'Best First'.
Re: File APPEND/OVERWRITE
by runrig (Abbot) on Feb 08, 2012 at 22:37 UTC
    If you are not using autodie, then you should be checking the status of each of your open statements, e.g.:
    open(my $fh, ">>", $myfile) or die "Failed to open $myfile: $!";
    Similarly, you should also check the status of chdir

      I tried the "or die" and the files all seem to be opening fine...(no death notices.. :-)

      I am wondering about something...earlier in my code, I have a line of code to do a "mkdir", which I assumed would act the same as if I was doing it by hand...i.e. if a mkdir is entered on a subdirectory that already exists, it will just go in with life...:

      foreach (@text) { $first = substr($_,0,11); mkdir "$first"; print "Copying $_ to $first.\n"; copy($_,"$first/$_"); unlink $_; }

      I am wondering if that "mkdir" is actually re-creating the sub-directories, which if so I guess would wipe out anything that was in there previously?

        I am wondering if that "mkdir" is actually re-creating the sub-directories
        Not really. Try it:
        mkdir 'XXX'; open my $FH, '>', 'XXX/1'; close $FH; mkdir "XXX" or warn $!; open my $FH, '<', 'XXX/1' or die $!; close $FH;
Re: File APPEND/OVERWRITE
by kielstirling (Scribe) on Feb 09, 2012 at 07:00 UTC

    You may want to look at opening the output files outside the main foreach loop. I suspect this is your problem. You are opening the same file each time you loop over @files. So the amount of entrees in the list will determine the amount of open file handles. If you must do it that way at least call close within the @files loop Also you should always check for any errors when opening a file.

    i.e
    open FH, "<file>" or die $!;

    This simple check can give you back hours of your life ;)

Re: File APPEND/OVERWRITE
by Anonymous Monk on Feb 08, 2012 at 22:36 UTC

    I am opening the receiving files as "append", but the files keep getting written over, not appended:

    Impossible :) Actually, what kind of operating system/file system are you using?

    It might be a good idea to use autodie; since you're currently not doing any error checking