[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Finding the common textual denominator



A lot shorter, less memory intensive, and probably faster;

#!/usr/bin/env perl

use warnings;
use strict;

my $string = '';
my $last   = '';

while (<>) {
  if ($last) {
    $string = &longest_substring_in_common($_,$last);
  } else {
    $last = $_;
  }
}

print "$string\n";

sub longest_substring_in_common {
  my ($a,$b) = @_;
  foreach my $i ( 0 .. length($a) ) {
    unless (substr($a,0,$i) eq substr($b,0,$i)) {
      return substr($a,0,($i-1));
    }
  }
}

-- 
xplanet -body earth -transpng a.png -geometry "300x300-0+0" -origin sun
My politics : http://satp.blogspot.com
Programmer

Attachment: signature.asc
Description: Digital signature


Reply to: