On Sun, Mar 06, 2005 at 10:21:36PM +0100, Bruno Hertz wrote:
> On Sun, 2005-03-06 at 22:10 +0100, Olle Eriksson wrote:
>
> > Is there any application that does this already, or will I have to write
> > something myself?
>
> Maybe there's something out there doing this, but writing
> it yourself isn't hard either. A very basic algorithm would
> be: if you have n strings s1, s2, ..., sn just take the
> first s1 and look wether all other begin with s1. If yes,
> you're done. If not, strip the last character off s1 and try
> again. At some point, you'll end up either with the empty string,
> or a nonempty 'common denominator'. And that's it.
>
> You can write it in awk, perl, C whatever you want. The
> algo above is of course primitive, so if you're string
> are millions of characters long and you have millions of
> them, you'll need to improve it. But 'til then, the above
> will do what you want.
>
> Regards, Bruno.
>
Hi Ollie,
here's my 2 yen.
-----------------------
#!/usr/bin/perl
use warnings;
use strict;
my $num=0;
my $b=1;
my @line;
while(<>){ $line[$num++]=$_; }
my @array_line_a = split //,$line[0];
my $min=length($line[0]) -1;
while ($b < $num && $min > 0) {
my $n;
my @array_line_b = split //,$line[$b];
for($n=0;$n<$min && $array_line_a[$n] eq $array_line_b[$n];){ $n++; }
$min = $n if ($n < $min);
$b++;
}
print "|";
for(my $m=0; $m < $min; $m++){
print $array_line_a[$m];
}
print "|";
print $min;
print "\n";
-----------------------
Kev
--
counter.li.org #238656 -- goto counter.li.org and be counted!
(__)
(oo)
/------\/
/ | ||
* /\---/\
~~ ~~
...."Have you mooed today?"...
Attachment:
signature.asc
Description: Digital signature