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

Dear Monks, In furthering my explorations into text mining and comparison, I'm looking to try and take two strings from a database (which represent sentences), split them into pieces if they are unequal. What I want to do is to compare $additional against $index and then print out the differences between the two which I'll ultimately write to a file though at the moment, I'd be happy to only print out the diffs.
#!c:\perl\bin\perl.exe use strict; use warnings; use DBI; my $index; my $additional; #Create the strings first my $db = DBI->connect('dbi:mysql:sequence:localhost', 'db', 'pass'); my $sth = $db->prepare('SELECT * FROM milton'); $sth->execute(); my $indexstring = $sth->fetchrow_array; $sth = $db->prepare('SELECT * FROM milton1'); $sth->execute(); my $additionaltext = $sth->fetchrow_array; print "$indexstring\n"; print "$additionaltext\n"; if ($indexstring eq $additionaltext) { print "lines match"; } else { print "lines do not match"; #$index = split //, $indexstring; #$additional = split //, $additionaltext; } $sth->finish; $db->disconnect();
The error message that I was getting says that use of @_ is deprecated in split but is there a cleaner, more efficient way of achieving my desired result? Thanks.
  • Comment on Comparing the individual characters in two strings to find their differences
  • Download Code

Replies are listed 'Best First'.
Re: Comparing the individual characters in two strings to find their differences
by grizzley (Chaplain) on Apr 03, 2008 at 07:40 UTC

    Shouldn't it look like:

    my ($additionaltext) = $sth->fetchrow_array;

    or

    my @data = $sth->fetchrow_array; my ($additionaltext) = @data;

    ? fetchrow_array() returns array, not scalar.

      Definitely. I once made the same mistake as the OP and it still worked, but one shouldn't count on that. There's no guarantee that it'll work with different database drivers, different versions of the driver being used, or even from call to call.

      Aside from that, the OP should consider using selectrow_array since only the first row is fetched.

      my $sth = $db->prepare('SELECT * FROM milton1'); $sth->execute(); my ($additionaltext) = $sth->fetchrow_array; $sth->finish;

      becomes

      my ($additionaltext) = $db->selectrow_array('SELECT * FROM milton1');

      I'm also annoyed by the inconsistency between $db and $sth. Use "h" for both or for neither. Actually, use "h" for both to be consistent with all the documentation out there.

        ikegami++ Pretty useful and I didn't know that syntax. Every day I'm learning something new :)
Re: Comparing the individual characters in two strings to find their differences
by ikegami (Patriarch) on Apr 03, 2008 at 08:07 UTC
      Thanks for the module pointers, I'll take a further look later. More Perl goodness to get into. :-) I was aware that the db syntax needed looking at in the morning as I was putting it together late last night.