Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: compare initial

by tybalt89 (Monsignor)
on Jul 11, 2018 at 22:53 UTC ( [id://1218353]=note: print w/replies, xml ) Need Help??


in reply to compare initial

Assuming the strings have the same length.

#!/usr/bin/perl # https://perlmonks.org/?node_id=1218306 use strict; use warnings; use Algorithm::Diff qw(traverse_balanced); compare( 'TTTATTT', 'TTTTTTT' ); compare( 'TTTTTTT', 'TBTTTTT' ); sub compare { my (@from, @to); print "compare '$_[0]' to '$_[1]', differences are"; traverse_balanced( [ @from = split //, $_[0] ], [ @to = split //, $_ +[1] ], { CHANGE => sub { print " '$from[$_[0]]'\@$_[0]->'$to[$_[1]]'\@$_[1] +" }, } ); print "\n"; }

Outputs:

compare 'TTTATTT' to 'TTTTTTT', differences are 'A'@3->'T'@3 compare 'TTTTTTT' to 'TBTTTTT', differences are 'T'@1->'B'@1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found