title: nested while loops in Perl
body: <p>I'm trying to open <code>file-a</code> and set each line as a regex.</p>
  
  <p>The number of times this regex occurs in <code>file-b</code> needs to be counted.</p>
  
  <p>I'm doing this by pushing each match of the regex in <code>file-b</code> into an array <code>@matches</code>.</p>
  
  <p>I then use <code>scalar(@matches)</code> to get the number of matches and print it to the screen.</p>
  
  <p>I'm trying to do this using nested <code>while</code> loops and it isn't working.</p>
  
  <p>What happens is that it also pushes the next regex (i.e. the next <code>$line</code> from <code>file-a</code>) into <code>@matches</code> instead of emptying<code>@matches</code>.</p>
  
  <pre><code>#!/usr/local/bin/perl -w
  
  use strict;
  
  my $domains = "domainer.txt";
  my $input   = "final_names.txt";
  my @matches;
  my $match;
  
  open NAMES, $input or die "Can't open $input, Perl says $!\n";
  
  while ( my $line = &lt;NAMES&gt; ) {
    chomp $line;
  
    my $id = $line;
    $id =~ s/\s//g;
  
    my $regex = $id;
  
    open DOMAINS, $domains or die "Can't open $domains, Perl says $!\n";
  
    while ( my $line = &lt;DOMAINS&gt; ) {
  
      if ($line =~ m/$regex/i) {
        push (@matches, $1);
      }
  
      $match = scalar (@matches);
      print "$match\n";
    }
  }
  
  close DOMAINS;
  close NAMES;
  </code></pre>