Edward Wijaya wrote:
Hi,

Hello,

I have following code that measure the substring.
It basically the number of positions of substrings
that occur (dotted count)

CASE A:
.......    .......   ......
GATTACGAGTGGCGCTCGTGTAACGGCA    #Score 21
GATTACG    GCGCTCG   AACGGCA


CASE B: .. .. #Score 4 GATTACGAGTGGCGCTCGTGTAACGGCA GG GG



But how come for CASE B, the code below returns Score = 2 instead of 4?


__BEGIN__ #!/usr/bin/perl -w use strict; use Data::Dumper;

my $s1 = 'GATTACGAGTGGCGCTCGTGTAACGGCA';
my @CASE_A  =  ('GATTACG','GCGCTCG','AACGGCA');  # 21
my @CASE_B  =  ('GG','GG');                      # 4

print &score($s1,[EMAIL PROTECTED]) , "\n";
print &score($s1,[EMAIL PROTECTED]) , "\n";


sub score { my ($str,$array) = @_; my %position_score;

  for my $frag (@{$array}) {
      my $idx = index($str, $frag) + 1;

      for my $pos ($idx .. $idx + (length($frag) - 1)) {
             $position_score{$pos} = 1;
      };
  };

  my $total_score = 0;
  for my $score (values %position_score) {
         $total_score += $score;
  };

  return $total_score;

};

__END__

It looks like this will do what you want:

sub score {
  my ( $str, $array ) = @_;
  my $total_score = 0;

  for my $frag ( @$array ) {
    my $len = length $frag;
    my $idx = index $str, $frag;

    if ( $idx >= 0 and substr $str, $idx, $len, '' ) {
      $total_score += $len;
      }
    }
  return $total_score;
  }




John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to