#!/usr/bin/perl use strict; #The Setup use warnings; use Text::CSV_XS; use DBI; my %conn_attrs = (RaiseError =>1, PrintError =>0, AutoCommit =>1); my $dbh = DBI->connect('DBI:mysql:pstsize;host=localhost'); my $sth = $dbh->prepare("SELECT DISTINCT nbname,ipaddr from sizehist order by nbname "); #Read the DB into a hash array $sth->execute(); my $dbData = {}; while (my $db_ref = $sth->fetchrow_hashref ()) { $dbData->{$db_ref->{nbname}}->{$db_ref->{ipaddr}} = 0; } #Read CSV file into hash array using CSV_XS module open my $io, "<", "use_me.txt" or die "use_me.txt: $!"; my $csv = Text::CSV_XS->new ({binary => 1, eol => "\n"}); while (my $row = $csv->getline ($io)) { if ( exists($dbData->{$row->[0]}->{$row->[1]}) ) { # Mark the db rows that match. $dbData->{$row->[0]}->{$row->[1]} = 1; } } close $io; # Build output arrays. my @match = (); my @dontmatch = (); for my $nbname (sort(keys(%$dbData))) { for my $ipaddr (sort(keys(%{$dbData->{$nbname}}))) { for my $nbname (sort(keys(%{$dbData->{$ipaddr}}))) { if ( $dbData->{$nbname}->{$ipaddr} == 1 ) { push (@match, [$nbname, $ipaddr]); } else { push (@dontmatch, [$nbname, $ipaddr]); } } } } # Output: print "These db rows match:\n"; map { print "$_->[0],$_->[1]\n" } @match; print "\n"; print "These db rows don't match:\n"; map { print "$_->[0],$_->[1]\n" } @dontmatch; exit;