in reply to Pattern match location finding

I prefer an easier to understand approach than diotalevi's - if your string is not too big, you can split it into an array where each element of the array is one character of the string, and then search through the array, something like this:
my $mystring = "AABAABBBB"; my @myarray = split //, $mystring; my $ct = 0; foreach my $element (@myarray) { if ($element eq "B") { push(@myoccurrences, $ct); } $ct++; }
NOTE that I'm sure that diotalevi's solution is much more efficient than mine. But this one I can understand simply, and that one I have to sit and think about ;-)

HTH.

Replies are listed 'Best First'.
Re: Re: Pattern match location finding
by pzbagel (Chaplain) on Feb 18, 2004 at 20:50 UTC

    Downside of your approach is that you assume the query string will always be one character. diotalevi's approach does not suffer that limitation. Definately works with the test case feloniousMonk presented, but fM refers to 'B' as a query string implying that it could be multiple characters in some cases.

      And in fact query string is always <= length of target string...

      Thanks for the help everyone, now I have some good suggestions to try out - they're all much better than my own earlier attempts. --