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

Jay Hannah jay at jays.net
Mon Feb 25 13:26:50 PST 2008


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





More information about the Omaha-pm mailing list