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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.