Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Matching bits of 2 strings

by japhy (Canon)
on Jan 22, 2002 at 04:50 UTC ( #140550=note: print w/replies, xml ) Need Help??

in reply to Matching bits of 2 strings

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:??;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140550]
[Tanktalus]: I'm sure it'll make perfect sense once we figure it out :)
[Lady_Aleena]: marioroy, not a quoting error. I can't run it on the command line. It is when I put it in qx() where it falls apart.
Discipulus hired to use Perl in Eataly? haired maybe
[choroba]: We're hiring
[Discipulus]: wow praha!
[Tanktalus]: choroba: do I have to move? :)
Discipulus has too much roots in a 2770 yo town
[marioroy]: I want a job after completing MCE in about a week. But feel that I've moved to the wrong place.
[choroba]: I fear so. We have offices in Prague, Brno, Saigon and San Francisco.
[Discipulus]: choroba if let your company to hire Tanktalus and marioroy ... what a big fishing!

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-04-23 20:08 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.