[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Find all the times a substring appears in a string




Problem:
  Find all the times $primer appears in $seq.

Solution:
  2 different solutions, below.

Feel free to benchmark them.  :)

j



$ cat j.pl
#!/usr/bin/perl

my $primer = 'GAATTCC';
my $seq = 'GAATTCCAAAAAAAAAAAAAGAATTCCTTTTTTTTTTTTTGAATTCCGGGGGGGGGGGGGGGGGGAATTCCTTTTTTTTTTTTTTTTTTTTTT';

print "Without using regular expressions (should be faster?):\n";
my $offset = 0;
while(1) {
  my $idx = index($seq, $primer, $offset);
  last if ($idx == -1);
  $offset = $idx + 1;
  print $idx + 1, "\n";
}

print "\n\nUsing regular expressions (fuzzy match possible):\n";

while ($seq =~ /($primer)/g) {
  print pos($seq) + 1 - length($primer), "\n";
}


$ perl j.pl
Without using regular expressions (should be faster?):
1
21
41
65


Using regular expressions (fuzzy match possible):
1
21
41
65