On 12/12/06, Helliwell, Kim <[EMAIL PROTECTED]> wrote:

Is there a function (perhaps in a library module) that would take two
strings and return the common substring (if any) contained in the
arguments?

You want the longest possible common substring? Or all of the longest
ones, if there's more than one?

I did something like this once. I used Perl's ability to treat strings
as bitstrings to xor one string against the other, at various
positions. Then I checked each xor result for a string of zeroes (i.e.
character matches) longer than any yet known, recording the length and
location of each match. Once I had checked all possible offsets that
could give a long-enough match, it was a simple matter to extract the
matching substring.

Of course, that method assumed ASCII characters were bytes. If you
might have non-byte characters in your data, this algorithom could
still work, but you'll need to code with more care.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to