Re: mini-golf: first differing position

"Bernie Cosell" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Organization Fantasy Farm Fibers
Message-ID <[email protected]>
On 20 Apr 2004 at 20:48, Craig S. Cottingham wrote:

> On Tue, 2004-04-20 at 19:26, Bernie Cosell wrote:
> > On 20 Apr 2004 at 19:57, [email protected] wrote:
> > > We want to find out the first position $n at which strings $x and $y
> > > differ....
> > 
> > How about [not golfing so the code is clearer]:
> >     my $diff = $x ^ $y;
> >     my ($len) = $diff =~ /^([\0])[^\0]/ ;
> >     return uhdef if not defined $len ;
> >     return length $len;'
> 
> I couldn't get that to work.

Really?  I cleaned it up a bit and it seems to work fine:

sub diff
{   my $diff = $_[0] ^ $_[1];
    my ($len) = $diff =~ /^([\0]*)[^\0]/ ;
    return undef if not defined $len ;
    return length $len;
}

  /Bernie\

-- 
Bernie Cosell                     Fantasy Farm Fibers
mailto:[email protected]     Pearisburg, VA
    -->  Too many people, too few sheep  <--
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.