title: Working through multiple files one at a time in perl?
body: <p>I have a perl script that opens a txt file, parses it so that the appropriate text is output to a csv file. I have working great now for one file, but I have loads of similar files to work through in the exact same way. I want to be able to just do this automatically so the code will work through file1.txt and parse the text I want to output.csv, then work through file2.txt and append this output to the same output.csv. I have included the relevan bits of my code below, excluding only the code that does the actual parsing within the while loop since I don't need to alter this. The input files are consistently named, e.g. file1.txt, file2.txt, file3.txt etc. and all reside in the same directory</p>
  
  <pre><code>my $mode = "none";
  open(my $infile,"&lt;","file1.txt") or die $!;
  open (my $outfile,"&gt;&gt;","output.csv") or die $!;
  while (&lt;$infile&gt;)
  {
      chomp; 
      if ($_ =~ /^Section 1/) {
          $mode = "sec1";
      }
      if ($_ =~ /^Section 2/) {
          $mode = "sec2";
      }
  
      if ($mode =~ "sec1") {
        $_=~ tr/,//d;
  
        if ($_ =~ /.\%$/){
          print $outfile $_;
          print $outfile "\n";
        }
        else{
          print $outfile $_;  
        }
  
      }    
  }
  
  close $infile;
  close $outfile;
  </code></pre>
  
  <p>The output file should resemble this (not this text obviously, I'm just highlighting that it the output must be appended, which I think I have covered by using >> as opposed to >)</p>
  
  <pre><code>this is from file 1
  this is from file 2
  this is from file 3
  </code></pre>