Quicksilver has asked for the wisdom of the Perl Monks concerning the following question:

As per an earlier suggestion, I have used Text::Diff to work out the diffs between two sets of word arrays but what I need to do is to split the results on the + sign to eventually wrap them up in XML for an AJAX application.
I get the result:
Scrooge never painted out old Marley's name + Nermal never painted out old Marley's name
What I'd like is:
Scrooge never painted out old Marley's name
Nermal never painted out old Marley's name
Is it possible to split on +? If not, can it be swapped and split on that instead?
#!c:\perl\bin\perl.exe use strict; use warnings; use DBI; use Text::Diff; use IO::File; my $diff; my $dbh; #Create the strings first $dbh = DBI->connect('dbi:mysql:dickens:localhost', 'admin', 'pass'); #my ($indexstring) = $dbh->selectrow_array('SELECT sentence FROM seque +nce'); #my ($additionaltext) = $dbh->selectrow_array('SELECT sentence FROM se +quence1'); my @indexstring; my @additionaltext; my $result; my $sth = $dbh->prepare("SELECT sentence FROM sequence"); $sth->execute; while (my @result = $sth->fetchrow_array) { push @indexstring,$result[0]; } $sth = $dbh->prepare("SELECT sentence FROM sequence1"); $sth->execute; while (my @result = $sth->fetchrow_array) { push @additionaltext,$result[0]; } $dbh->disconnect(); ################################ #Compare strings or arrays ################################ #Use Text::Diff for arrays. #Need a regex to get the relevant data out my @difference = diff \@indexstring, \@additionaltext; foreach $diff(@difference) { my $unequal = split(/+/, $diff); print "$unequal\n"; }
Is there, perhaps, a better of doing this (though this nearly gives the me the results I'd love)?

Replies are listed 'Best First'.
Re: Splitting on + after using Text::Diff
by Corion (Patriarch) on Apr 04, 2008 at 10:44 UTC

    If you don't like the textual results of Text::Diff, maybe you want (as mentioned in the Text::Diff documentation) the module that Text::Diff uses to do its work, Algorithm::Diff?

    That way, you can conveniently serialize the results to XML and hand them off to your JavaScript client.

      You're right and the results look a lot better and cleaner.
Re: Splitting on + after using Text::Diff
by deibyz (Hermit) on Apr 04, 2008 at 10:57 UTC
    The first param to split is a regexp, so you have to scape the + so it doesn't act as a quantifier:

    my $unequal = split(/\+/, $diff);