Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: compare initial

by AnomalousMonk (Archbishop)
on Jul 11, 2018 at 14:05 UTC ( [id://1218314]=note: print w/replies, xml ) Need Help??


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:

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
BTW: I, too, find your OP a bit vague.

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 sundialsvc4 (Abbot) on Jul 11, 2018 at 14:56 UTC
    The only possible fly in that ointment would be Unicode (UTF-8,16) because these results are necessarily byte offsets.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218314]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-03-29 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found