title: Refining perl code for sliding window approach
body: <p>I am working with a file (fasta file), here is the format-</p>
  
  <pre>
  >chr1
  AACCCCCCCCTCCCCCCGCTTCTGGCCACAGCACTTAAACACATCTCTGC
  CAAACCCCAAAAACAAAGAACCCTAACACCAGCCTAACCAGATTTCAAAT
  TTTATCTTTAGGCGGTATGCACTTTTAACAAAAAANNNNNNNNNNNNNNN
  NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
  GCCCATCCTACCCAGCACACACACACCGCTGCTAACCCCATACCCCGAAC
  CAACCAAACCCCAAAGACACCCCCCACAGTTTATGTAGCTTACCTCNNNN
  >chrM
  GATCACAGGTCTATCACCCTATTAACCACTCACGGGAGCTCTCCATGCAT
  TTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTG
  GAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATT
  CTATTATTTATCGCACCTACGTTCAATATTACAGGCGAACATACCTACTA
  AAGTGTGTTAATTAATTAATGCTTGTAGGACATAATAATAACAATTGAAT
  GTCTGCACAGCCGCTTTCCACACAGACATCATAACAAAANAATTTCCACC </pre>
  
  <p>I want to use sliding window approach (non overlapping windows, size =50). I want to find the coordinates for each character with in 50bp window but not including N's. The output for first chr1 should be-></p>
  
  <pre>
  chr1   0       50
  chr1   50      100
  chr1   100     215 
  chr1   215     265</pre>
  
  <p>The code  is -</p>
  
  <pre><code>use warnings; 
  *ARGV or die "No input file specified";
  open *first, '&lt;',$ARGV[0] or die "Unable to open input file: $!";
  $start=1;
  while(&lt;first&gt;) {
      chomp;
      if ( /(&gt;)(\w)/ ) {   #taking lines which have name of chromosome
      @arr=split("&gt;");  #splitting at "&gt;" character and in $arr[1], there is chr name now
  
  
          if (defined @array){
  
              foreach (@array){
              $length++;      
  
                  if($_ ne N){
                      $non++;
                      if ($non == 50){
  
                      print $chr,"\t",$start,"\t",$length,"\n";
                      $start=$length;
                      $non=0;
  
                      }
                  }       
              }
          }
  
          undef @array;  
          $length=0;
          $non=0;
          $start=0;
      }
  
      else {
  
          @count=split(//, $_); #splitting each character in line
  
          push(@array,@count);  #storing each character in array till we find next chromosome
  
          $chr=$arr[1];
      } 
  
  }
  
  
  
  
  foreach (@array){
          $length++;
  
            if($_ ne N){
            $non++;
                 if ($non == 50){
  
          print $chr,"\t",$start,"\t",$length,"\n";
          $start=$length;
          $non=0;
  
                 }
            }
  
  }
  </code></pre>
  
  <p>The thing is my fasta file is big and this code is taking lot of memory and time. Can you please give suggestions how can I make it fast using less memory.</p>
  
  <p>Thanks</p>