Hi Paul

"Rob Anderson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
>
> "Paul Archer" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>
> [snip]
>
> >
> > If anyone can offer general improvements to that approach, that'd be
> > great--but what I'm really interested in is finding the first element
that
> > differs, and maybe the last as well. If there were any easy way to find
> > that, say, the first character that differs is at position 120, and the
> last
> > character that differs is at position 135, then I only have to loop
> through
> > 15 characters, not 160.
>
> Probably the quickest way to do this in perl is as follows....
>
> ----------------------------------------
> #!/perl -w
> use strict;
> my $string1 = "The quick brown fox.";
> my $string2 = "The Kuick brUwn fox.";
>
> my $string_xor = ("$string1" ^ "$string2");
>
> $string_xor =~ /^(\0*)/;
> print "Number of equal bytes at the front of the string = " . length($1) .
> "\n";
> $string_xor =~ /(\0*)$/;
> print "Number of equal bytes at the end of the string = " . length($1) .
> "\n";
> ----------------------------------------
>
> the xor op (^) will return a string, every matching byte of which will be
> null, every mismatching byte will have some bit (literaly) set.
>
> so the all first regex does is match the all the null bytes it can find
from
> the front of the string, the length of the string is equivalent to the
> number of identical chars at the front.
> The second regex is the same, just working from the end.
>
> You could extend this and translate all the non-null values to something
you
> could then search for that value with index. This would give you the
> positions of all the bytes that were different...
>
> my $string_xor = ("$string1" ^ "$string2");

missed the actual traslation...  :-$
$string_xor =~ s/[^\0]/X/g;

> my $pos = 0;
> while ( ($pos = index($string_xor, 'X', $pos)) != -1) {
>    print "==> $pos\n";
>    $pos++;
> }
>
> Hope this helps,
>
> Rob
>
> >
> > TIA,
> >
> > paul
> >
> >
> > ---------------------------------------------------------------------
> > On the side of the software box, in the "System Requirements" section,
> > it said "Requires Windows 95 or better".  So I installed Linux.
> > ---------------------------------------------------------------------
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to