The "standard" way to compute the longest left-anchored common substring would be to compare character for character of both strings until they differ. However, in Perl it's relatively cumbersome (compared to C for example) to access a single character in a string (via the substr builtin), so this approach is quite slow (even for rather short strings like your sample).

For comparison, here's the results of a quick benchmark, so you'll actually appreciate the performance of the XOR method suggested by Corion:

#!/usr/bin/perl use Benchmark qw(cmpthese); use Inline C; my $s1 = '/home/usernames/doejohnwilson'; my $s2 = '/home/usernames/doejanemary'; cmpthese(-1, { 'standard' => sub { my $pos = 0; $pos++ while substr($s1, $pos, 1) eq substr($s2, $pos, 1); my $result = substr($s1, 0, $pos); # die unless $result eq "/home/usernames/doej"; }, 'fancy-XOR' => sub { my $common = $s1 ^ $s2; my $common_length = 0; if ($common =~ /^(\x00+)/) { $common_length = length $1; }; my $result = substr($s1, 0, $common_length); # die unless $result eq "/home/usernames/doej"; }, 'standard-XS' => sub { my $common_length = left_anchored_common_substr_len($s1, $s2); my $result = substr($s1, 0, $common_length); # die unless $result eq "/home/usernames/doej"; } }); __END__ __C__ int left_anchored_common_substr_len(char* s1, char* s2) { int c = 0; while (*s1++ == *s2++) { c++; } return c; }

Results:

Rate standard fancy-XOR standard-XS standard 109227/s -- -77% -92% fancy-XOR 468114/s 329% -- -64% standard-XS 1291788/s 1083% 176% --

Update: added an XS routine (via Inline::C) that implements the aforementioned "standard" approach in C.


In reply to Re: Print left-anchored similarities between two strings by almut
in thread Print left-anchored similarities between two strings by miketosh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.