title: Read file and store in array in Perl?
body: <p>I would like to read a file and to produce a numbers of arrays depending on how many chains (M,N,O,..) it would have.</p>
  
  <p>Following is a part of a file:</p>
  
  <pre><code>SEQRES   1 M  312  ALA ALA ASP PRO LYS LEU LEU LYS ALA ALA ALA GLU ALA
  SEQRES   2 M  312  SER TYR ALA PHE ALA LYS GLU VAL ASP TRP ASN ASN GLY
  SEQRES   3 M  312  ILE PHE LEU GLN ALA PRO GLY LYS LEU GLN PRO LEU GLU
  SEQRES   4 M  312  ALA LEU LYS ALA ILE ASP LYS MET ILE VAL MET GLY ALA
  SEQRES   5 M  213  SER PHE ASN ARG ASN
  
  SEQRES   1 N  312  ASP GLU ILE GLY ASP ALA ALA LYS LYS LEU GLY ASP ALA
  SEQRES   2 N  312  SER TYR ALA PHE ALA LYS GLU VAL ASP TRP ASN ASN GLY
  SEQRES   3 N  312  ILE PHE LEU GLN ALA PRO GLY LYS LEU GLN PRO LEU GLU
  SEQRES   4 N  312  ALA LEU LYS ALA ILE ASP LYS MET ILE VAL MET GLY ALA
  SEQRES   5 N  312  ALA ALA ASP PRO LYS LEU LEU LYS ALA ALA ALA GLU ALA
  SEQRES   6 N  312  VAL THR SER ARG ALA ASP TRP ASP ASN VAL
  
  SEQRES   1 O  312  HIS HIS LYS ALA ILE GLY SER ILE SER GLY PRO ASN GLY
  SEQRES   2 O  312  SER TYR ALA PHE ALA LYS GLU VAL ASP TRP ASN ASN GLY
  SEQRES   3 O  312  ILE PHE LEU GLN ALA PRO GLY LYS LEU GLN PRO LEU GLU
  SEQRES   4 O  312  ALA LEU LYS ALA ILE ASP LYS MET ILE VAL
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>my @seq;
  my $string="";
  my @seqFile;
  my $file=&lt;&gt;;
  open(FILE, "$file");
  while (my $line=&lt;FILE&gt;){
      if ($line =~ /^SEQRES/) {
          chomp $line;
          push @seq, [split (/\s+/, $line)] ;
      }
  }
  close(FILE);
  for my $i (0..$#seq) {
      my $ob =$seq[$i][2];
      if ($seq[$i][2] eq $ob ){
          for (my $j=4;$j&lt;=$#{$seq[$i]};$j++) {
              my $temp= $seq[$i][$j];
              $string .= $temp;
          }
          $ob = $seq[$i][2];
          last;
      }
      push @seqFile, $ob;
      push @seqFile, $string;
      $string = ''; #string needs to be empty to store new lines
  }
  </code></pre>
  
  <p>With the above sample: 3 arrays M(:)ALAALAASP:...., N(:)ASPGLU.., O(:)HISHISLYS...</p>
  
  <p>I managed to make all SEQRES in one string, but which is not what I wanted.</p>
  
  <p>Somewhere I need to put a <code>if(){}</code> and to check <code>M &lt;=&gt; N</code> and <code>N &lt;=&gt; O</code> are different. Then save the string and start a string and array. But it keeps accumulating as many times of same string as $#seq. Or if I move the position of one <code>}</code> then it does not store anything, Either gives me error messages. How can I do this?</p>