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 | |
by he204035 (Novice) on Feb 09, 2012 at 17:24 UTC | |
by choroba (Cardinal) on Feb 09, 2012 at 22:39 UTC | |
|
Re: File APPEND/OVERWRITE
by kielstirling (Scribe) on Feb 09, 2012 at 07:00 UTC | |
|
Re: File APPEND/OVERWRITE
by Anonymous Monk on Feb 08, 2012 at 22:36 UTC |