in reply to Characters in disguise

I'm sure there are many nasty traps here, but you could take a look at Text::Diff.

use strict; use warnings; use Algorithm::Diff qw(diff); my @strs = ( ['ABCÅD', 'ABCD'], ['ABCÄD', 'ABCëëD'], ['ABCááD', 'ABCèD'], ); my %xlate; for (@strs) { my ($org, $mod) = @$_; my @orgSeq = split //, $org; my @modSeq = split //, $mod; my (@diffs) = diff (\@orgSeq, \@modSeq); for my $diff (@diffs) { my @sub = grep {$_->[0] eq '-'} @$diff; my @add = grep {$_->[0] eq '+'} @$diff; next if ! @sub; # Bogus added characters $xlate{join '', map {$_->[2]} @sub} = (join '', map {$_->[2]} +@add) || ''; } } print "'$_' => '$xlate{$_}'\n" for sort keys %xlate;

Prints:

'Ä' => 'ëë' 'Å' => '' 'áá' => 'è'

Update: changed bogus use Text::Diff to use Algorithm::Diff and clean up code to suit.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Characters in disguise
by Anonymous Monk on Jun 01, 2006 at 23:24 UTC
    Splendid!

    This seems to be the solution. If not a final solution, so very close. I notice we don't need Text::Diff as your code is just calling Algorithm::Diff::diff(). So I replaced use Text::Diff with use Algorithm::Diff.

    Thank you, and also thanks to tye for his reply and for the module Algorithm::Diff.

    /L

      Sigh. I started looking at Text::DIff, then decided it wasn't what was needed so I changed the code to use Algorithm::Diff without remembering to change the use :(.

      Glad it helped. I've learned something getting it going anyway. :)


      DWIM is Perl's answer to Gödel