Yes there is another way of doing this, by using SQL select's with the DBD::CSV module. I have constructed a little example below.
use strict;
use DBI;
use DBD::CSV;
use IO::File;
# Prepare the csv file #1
my $p; { my $f = new IO::File "passwd", "r"; local $/; $p = <$f>; }
{ my $t = new IO::File "passwd.txt", "w";
print $t uc("login:x:uid:gid:desc:home:shell\n"), $p; }
# Prepare the csv file #2
my $p; { my $f = new IO::File "passwd2", "r"; local $/; $p = <$f>; }
{ my $t = new IO::File "passwd2.txt", "w";
print $t uc("login:x:uid:gid:desc:home:shell\n"), $p; }
# Connect to CSV database
my $dbh = DBI->connect("DBI:CSV:csv_sep_char=\\:") or die "Cannot conn
+ect: " . $DBI::errstr;
$dbh->{'csv_tables'}->{'passwd'} = { 'file' => 'passwd.txt' };
$dbh->{'csv_tables'}->{'passwd2'} = { 'file' => 'passwd2.txt' };
my $sth = $dbh->prepare("SELECT p1.login FROM passwd p1, passwd2 p2
WHERE (p1.login=p2.login) AND (p1.uid=p2.uid)"
+);
$sth->execute();
# Get the matching id's
my $matched;
while (my $res = $sth->fetchrow_hashref())
{
$matched .= ",'" . $res->{LOGIN} . "'";
}
$sth->finish;
# Get the unmatched id's in passwd2
$sth = $dbh->prepare("SELECT login FROM passwd2 WHERE login not in ("
.substr($matched, 1) . ")");
$sth->execute();
while (my $res = $sth->fetchrow_hashref())
{
print "Unmatched accounts in passwd2: $res->{LOGIN}\n";
}
$sth->finish;
# Get the unmatched id's in passwd
$sth = $dbh->prepare("SELECT login FROM passwd WHERE login not in ("
.substr($matched, 1) . ")");
$sth->execute();
while (my $res = $sth->fetchrow_hashref())
{
print "Unmatched accounts in passwd2: $res->{LOGIN}\n";
}
$dbh->disconnect;
unlink("passwd.txt", "passwd2.txt");
It's not the most efficient method in this particular case, but the benefit will be more significant on more complex sort of comparisons.
|