Re: mini-golf: first differing position

Xavier Noria <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
On Apr 21, 2004, at 1:57, <[email protected]> wrote:

> We want to find out the first position $n at which strings $x and $y
> differ.  If $x eq $y, then $n is undef.  If $x ne $y but $x is a
> prefix (aka initial segment) of $y, then $n should be equal to length
> $x.  The operation must preserve both $x and $y (i.e. destructive
> operations are disallowed).
>
> By way of baseline, here's an utterly uninspired solution:
>
> $n=0;while(($c=substr($x,$n,1)||undef $n) && $c eq 
> substr($y,$n++,1)){;}

Second attempt:

     $n=$x eq$y?$z:do{1while$x=~/./sg&$y=~/\G$&/g;$-[0]}

I think this is OK.

-- fxn
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.