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.


In reply to Re: Compare csv file fields? by Roger
in thread Compare csv file fields? by traveler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.