in reply to An efficient way to gather a common portion of several strings' beginnings

Here's a version using List::Util. Pretty straight-forward.

#! /usr/bin/perl use strict; use warnings; use List::Util q(first); sub csp { my ($n, $s) = (-1, shift // return); while (++$n < length($s)) { my $c = substr($s,$n,1); last if first { substr($_,$n,1) ne $c } @_; } substr($s,0,$n); } print csp(qw(ahhaaaaa aha ahem));

But sorting the strings by length could still prove beneficial, esp. if the data is already thus structured.

  • Comment on Re: An efficient way to gather a common portion of several strings' beginnings
  • Download Code