I really liked the one-liner, just that it doesn't work - it fails for 000 720

Here's what I came up with. Is doesn't look that efficient, but it has the advantage of using no regexen for the actual comparison (only for split).
#!/usr/bin/perl -wl use strict; use warnings; sub Prefix { { my $e = scalar(@_); return '' if ($e == 0); return $_[0] if ($e == 1); } my $minlen; my @d = map { my @split = split(//, $_); my $l = scalar(@split); if (defined($minlen)) { $minlen = $l if ($l < $minlen); } else { $minlen = $l; } \@split; } @_; my $p = ''; my $i = 0; while($i < $minlen) { my $c = $d[0]->[$i]; return $p unless ($c); for (my $j = 1; $j <= $#d; $j++) { return $p unless ($c eq $d[$j]->[$i]); } $p .= $c; $i++; } $p } while (<DATA>) { print "$_=> ", Prefix(split ' ',$_); } __DATA__ model4run1 model2run1 model4run2 model1run1 model4run1 model2run1 model4run2 abbot model4run1 model2run1 abbot model1run1 model4run1 model2run1 model4run2 monk model4run1 model2run1 monk model1run1 testing terse testing time monk model4run1 model2run1 model1run1 000 720 a aa abc a aa a aa aaa ab ba bb

In reply to Re^4: Find common prefix from a list of strings by slink
in thread Find common prefix from a list of strings by Albannach

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.