I think this may indeed be a case where the algorithm is important.

I didn't see the magic words `Boyer-Moore' in your message anywhere; I
suspect that you want to look up the Boyer-Moore string-search
algorithm.

It's probably what Perl implements.

The idea is that you precompile a table on each character in your
shorter string, that tells you how far that character is from the end
of the string.  Then you start comparing at the end, rather than the
beginning, of the shorter string (you compare it to the corresponding
position in the longer string).  If you get a mismatch, you check for
the character in the longer string in your table, and that tells you
how far you can advance the search.

For example, let's say you're trying to match 

   the end

with 

   it is never over until you see the words the end.

You make a table of the letters in "the end":

  d 0
  n 1
  e 2
<sp>3
  h 5
  t 6

(Notice that since the 'e' occurs twice, you use the one closer to the
end.)

Anything not in the table lets you advance the whole length of the
short string.

You start out:

   it is never over until you see the words the end.
   the end
         ^
The characters don't match, so you look up "n" in your table.  You get
to advance one position:

   it is never over until you see the words the end.
    the end

Now you see a mismatch, and look up the "e"; you can advance 2
positions:

   it is never over until you see the words the end.
      the end

Mismatch, "e" again, two more:

   it is never over until you see the words the end.
        the end

Space this time, advance 3:

   it is never over until the end.
           the end

"e" again, advance 2:

   it is never over until you see the words the end.
             the end

space, advance 3:

   it is never over until you see the words the end.
                the end

"t", advance 6:

   it is never over until you see the words the end.
                      the end

"u", not in the table, advance 7:

   it is never over until you see the words the end.
                             the end

"h", advance 5:

   it is never over until you see the words the end.
                                  the end

"r", not in the table, advance 7:

   it is never over until you see the words the end.
                                         the end

space, advance 3:

   it is never over until you see the words the end.
                                            the end

and you've matched.

This is supposed to be a good algorithm.

I'm not up on loop yet (and may never be) so I'm not sure quite what
your code is doing, but it doesn't look like it's doing Boyer-Moore.

Hope this helps,

-- 
Fred Gilham                     [EMAIL PROTECTED]
"In the 20th century, more citizens were killed by their own
governments than by foreign enemies....totalitarianism first of all
regards its own people as the enemy." --- Arnold Beichman

Reply via email to