title: Perl - How to Read, Filter & Output results
body: <p><strong>scenario:</strong> I am a Jr. C# developer, but recently (3 days) began learning Perl for batch files. I have a requirement to parse through a text file, extract some key data, then output the key data to a new text file. As seems to always be the case, there are butt loads of fragmented examples on the net regarding how to 'read' from a file, 'write' to a file, 'store' line by line into an array, 'filter' this and that, yadda yadda, but nothing discussing the entire process of read, filter, write. Trying to splice examples from the net together is no good, because none seem to work together as coherent code. Coming from C#, Perl's syntax structure is hella confusing. I just need some advice on this process. </p>
  
  <p>My objective is to parse a text file, single out all lines similar to the one below, by date, and output only the first 8 digits of the 2nd number group and 5 digits from the 3rd number group to a new text file.  </p>
  
  <pre><code>11122 20100223454345 ....random text..... [keyword that identifies all the 
  entries I need]... random text 0.0034543345 
  </code></pre>
  
  <p>I know regex is likely the best option, and have most of the expression written, but it does not work in Perl!  </p>
  
  <p><strong>Question:</strong> Could someone please show a simple (dummy) example of how to read from, filter (using dummy regex) the file, then output the (dummy) results to a new file? I'm not concerned with functional details, I can learn those, I just need the syntax structure Perl uses. For example:</p>
  
  <pre><code> open(FH, '&lt;', 'dummy1.txt')
   open(NFH, '&gt;', 'dummy2.txt')
  
   @array; or $dumb;
   while(&lt;FH&gt;) 
   {
      filter each line [REGEX] and shove it into [@array or $dumb scalar]
   } 
   print(join(',', @array)) to dummy2.txt
   close FH;
   close NFH;
  </code></pre>
  
  <p><strong>Note:</strong> For various reasons, I cannot paste my source code in here, sorry. Any help is appreciated. </p>
  
  <p><strong>UPDATE: ANSWER:</strong> </p>
  
  <p>Much thanks to all those who provided insight into my issue. After reading through you replies, as well as conducting further research, I learned that there are dozens of ways to accomplish the same task in Perl(which I am not a fan of). In the end, this is how I solved the problem, and IMO it's the cleanest, and most succinct, solution for those having similar struggles. Thanks again for all the help.  </p>
  
  <pre><code>      #======================================================================
    # 1. READ FILE:   inputFile.txt
    # 2. CREATE FILE: outputFile.txt
    # 3. WRITE TO:    outputFile.txt IF line matches REGEX constraints
    # 4. CLOSE FILES: outputFile.txt &amp; inputFile.txt
    #==========================================================================
  
    #1
    $readFile = 'C:/.../.../inputFile.txt';
    open(FH, '&lt;', $readFile) or Error("Could not read file ($!)");
  
    #2
    $writeFile = 'C:/.../.../outputFile.txt';
    open(NFH, '&gt;', $writeFile) or Error("Cannot write to file ($!)");
  
    #3
    @lines = &lt;FH&gt;;
    LINE: foreach $line (@lines)
    {
       if ($line =~ m/(201403\d\d).*KEYWORD.*time was (\d+\.\d+)/)
       {
          $date = $1;
          $elapsedtime = $2;
          print NFH "$date,$elapsedtime\n";
       }
    }
  
    #4
    close NFH;
    close FH;
  </code></pre>