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

Re: Finding the common textual denominator



On Mon, 2005-03-07 at 15:59 +0100, Bruno Hertz wrote:

> Without testing it, shouldn't it be 
> 
>     $last = &longest_substring_in_common($_,$last);
> 
> instead of
> 
>     $string = &longest_substring_in_common($_,$last);
> ?
> 
> Regards, Bruno.

OK, what about this

#!/usr/bin/env perl

use warnings;
use strict;

my $last=<>;
chomp $last;

while (<>) {
    last if not $last;
    chomp;
    $last = longest_substring_in_common($last,$_);
}
print "$last\n";

sub longest_substring_in_common {

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






Reply to: