in reply to compare initial
Further to the hint in haukex's post: The bitwise-xor of equal length strings (update: assumed to be ASCII strings; see sundialsvc4's point here++) is a neat differencing trick. Here's an example that extracts zero-based offsets of differing characters:
BTW: I, too, find your OP a bit vague.c:\@Work\Perl\monks>perl -wMstrict -le "my ($x, $y, $z) = qw(TTTATTT TTTTTTT TBTTTTT); ;; for my $ar ([ $x, $y ], [ $x, $z ], [ $y, $z ], ) { my ($p, $q) = @$ar; my $d = $p ^ $q; my @offsets; push @offsets, $+[0]-1 while $d =~ m{ \G \x00* [^\x00] }xmsg; print $p; print $q; if (@offsets) { printf qq{diff(s) at offset(s) %s \n}, join q{, }, @offsets; } else { print qq{no diffs \n}; } } " TTTATTT TTTTTTT diff(s) at offset(s) 3 TTTATTT TBTTTTT diff(s) at offset(s) 1, 3 TTTTTTT TBTTTTT diff(s) at offset(s) 1
Update: Arrrgh! Use m{ [^\x00] }xmsg instead of m{ \G \x00* [^\x00] }xmsg (simpler == better).
Double Arrrgh! Even simpler: Instead of
push @offsets, $+[0]-1 while $d =~ m{ [^\x00] }xmsg;
use
push @offsets, $-[0] while $d =~ m{ [^\x00] }xmsg;
(See perlvar for @+ and @- regex special variables.)
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compare initial
by locked_user sundialsvc4 (Abbot) on Jul 11, 2018 at 14:56 UTC |