This is the "leading common substring" problem. There's been work in Perl done on it. Here's a relatively fast solution:
# leading common substring
# returns length of LCS
# requires Perl 5.6+
sub lcs {
($_[0] ^ $_[1]) =~ /\0*/;
return $+[0];
}
If you're not fortunate enough to have Perl 5.6 yet, here's a compatible approach:
# leading common substring
# returns length of LCS
# requires Perl 5.6+
sub lcs {
($_[0] ^ $_[1]) =~ /(\0*)/;
return length $1;
}
Update: in case you were worried, I do not need a
^ anchor in these regexes, because
* will gladly match zero times at the beginning of the string if it has to.
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;