title: Counting number of pattern matches in Perl
body: <p>I am VERY new to perl, and to programming in general. 
  I have been searching for the past couple of days on how to count the number of pattern matches; I have had a hard time understanding others solutions and applying them to the code I have already written. </p>
  
  <p>Basically, I have a sequence and I need to find all the patterns that match [TC]C[CT]GGAAGC </p>
  
  <p>I believe I have that part down. but I am stuck on counting the number of occurrences of each pattern match. Does anyone know how to edit the code I already have to do this? Any advice is welcomed. Thanks! </p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  use diagnostics;
  
  # open fasta file for reading 
  unless( open( FASTA, "&lt;", '/scratch/Drosophila/dmel-all-chromosome-    r6.02.fasta' )) {
      die "Can't open dmel-all-chromosome-r6.02.fasta for reading:", $!;
  }
  
  #split the fasta record
  local $/ = "&gt;";
  
  #scan through fasta file 
  while (&lt;FASTA&gt;) {
      chomp;
      if ( $_ =~ /^(.*?)$(.*)$/ms) {
              my $header = $1;
              my $seq = $2;
              $seq =~ s/\R//g; # \R removes line breaks 
                      while ( $seq  =~ /([TC]C[CT]GGAAGC)/g) {
                              print $1, "\n";
              }
      }
  }
  </code></pre>
  
  <p>Update, I have added in </p>
  
  <pre><code>my @matches = $seq =~ /([TC]C[CT]GGAAGC)/g;
                              print scalar @matches; 
  </code></pre>
  
  <p>In the code below. However, it seems to be outputting 0 in front of each pattern match, instead of outputting the total sum of all pattern matches.  </p>
  
  <pre><code>while (&lt;FASTA&gt;) {
      chomp;
      if ( $_ =~ /^(.*?)$(.*)$/ms) {
              my $header = $1;
              my $seq = $2;
              $seq =~ s/\R//g; # \R removes line breaks 
                      while ( $seq  =~ /([TC]C[CT]GGAAGC)/g) {
                              print $1, "\n";
                              my @matches = $seq =~ /([TC]C[CT]GGAAGC)/g;
                              print scalar @matches;
      }
      }
  }
  </code></pre>
  
  <p>Edit: I need the output to list ever pattern match found. I also need it to find the total number of matches found. For example:</p>
  
  <p>CCTGGAAGC</p>
  
  <p>TCTGGAAGC</p>
  
  <p>TCCGGAAGC</p>
  
  <p>3 matches found </p>