Here's how I would do it:

use strict; use Text::CSV_XS; use DBI; my $csv_inputfile = "input.csv"; # change me my %data; # hash for storing csv data my $csv = Text::CSV_XS->new; open my $fh, '<', $csv_inputfile or die "can't open $csv_inputfile: $! + \n"; while (my $line = <$fh>) { $line =~ s/^\s+//; # strip leading whitespace next unless $line; # ignore blank lines # assume CSV file looks like: ip, user, nbname if ( $csv->parse($line) ) { my ($ip, $user, $nbname) = $csv->fields(); next unless $ip; $data{$ip} = { USER => $user || '', NBNAME => $nbname || '', }; } else { my $error = $csv->error_diag(); print "Parse error: line $. : $error.\n"; } } close $fh; my $user = "db_user"; # change me my $pass = "db_pass"; # change me my $dbh = DBI->connect( "dbi:mysql:pstsize", $user, $pass, {RaiseError => 1, AutoCommit => 1}, ); unless ($dbh) { die "Could not get database handle."; } my $sth = $dbh->prepare( select_data() ); eval { $sth->execute(); }; if ($@) { die "Database error: ", $dbh->errstr; } print "<html><body><pre>\n"; while ( my ($ip, $user, $nbname) = $sth->fetchrow ) { unless ($user) { print "DB missing USER for $ip."; if ($data{$ip}{'USER'}) { print ".. but CSV file has value: $data{$ip}{'USER'}."; } print "\n"; } unless ($nbname) { print "DB missing NBNAME for $ip."; if ($data{$ip}{'NBNAME'}) { print ".. but CSV file has value: $data{$ip}{'NBNAME'}."; } print "\n"; } } print "</pre></body></html>"; $sth->finish; $dbh->disconnect; sub select_data { return <<" END_SQL"; SELECT DISTINCT ipaddr, user, nbname FROM sizehist ORDER BY ipaddr END_SQL }

Notes:

Good luck!


In reply to Re: Need to search through a hashref array to find empty, null, or otherwise translucent key values. by scorpio17
in thread Need to compare 2 hash values by carlo1973

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.