in reply to Double check for positions

since the next match blends into the first one

Actually it doesn't.

use strict; use warnings; use Test::More tests => 2; my $pos = match_positions ('GCTTCTTGC', 'AGCTTCTTGCGCTTCTT'); is ($pos, '2 9 ', 'Matched'); # This fails because you have only one o +ccurrence. $pos = match_positions ('GCG', 'AGCGCGT'); is ($pos, '2 4 ', 'Matched'); sub match_positions { my ($regexp, $sequence) = @_; my @positions = ( ); while ( $sequence =~ m/$regexp/ig ) { my $newpos = pos($sequence) - length($&) + 1; push @positions, $newpos; pos($sequence) = $newpos; } return "@positions "; }

Update: fixed the test count to include the second easier-to-follow example.

Replies are listed 'Best First'.
Re^2: Double check for positions
by bisimen (Acolyte) on Oct 18, 2017 at 13:22 UTC

    This works, thanks!

    But, what do you mean by "I only have one occurrence"? Or explain it a little bit deeper...

    And, this line:

    pos($sequence) = $newpos;
    Why is it needed?

      I mean that your search pattern GCTTCTTGC only appears once in AGCTTCTTGCGCTTCTT. If you think it appears twice, you are mistaken.

      The pos reset is needed in case there are overlapping pattern matches (which is what I've taken your question to be about, despite the sample data). In that case the pos marker needs to be backtracked to one place after the start of the last match to cope with that.

        Oh yeah, I forgot to remove the one "GC" is my example input... sorry.