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.
In reply to File APPEND/OVERWRITE by he204035
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |