Do the comparison with exists. I would run the LDAP routine first then the DB routines and use a hash lookup for the region id rather than a sub-select.

use DBI; use Data::Dump 'pp'; my $dbh = DBI->connect($dbdsn, $dbuserid, $dbpassword, {RaiseError => 1,PrintError => 1}) or die "Connection Error: $DBI::errstr\n"; # users my $sql_u = 'SELECT id from users'; my $dbUsers = $dbh->selectall_hashref($sql_u,'id'); print "Users\n"; pp $dbUsers; # regions my $sql_r = 'SELECT id,name FROM regions'; my $regions = $dbh->selectall_hashref($sql_r,'name'); print "Regions\n"; pp $regions; # prepare insert sql my $sql_i = "INSERT INTO users (id, name, displayname, regionid) VALUES (?,?,?,?)"; my $sth_i = $dbh->prepare($sql_i); for my $uid (keys %$ldapUsers) { # check if user not exists in db if (! exists $dbUsers->{$uid}){ my $ldap = $ldapUsers->{$uid}; my @f = ($uid); $f[1] = $ldap->{'name'}; $f[2] = $ldap->{'displayName'}; $f[3] = $regions->{$ldap->{'defaulteRegion'}}{'id'}; # uncomment the execute after testing #$sth_i->execute(@f); print "Insert record : ".(join " | ",@f,"\n"); } }
poj

In reply to Re^2: Check if the first hash value is present in second hashmap by poj
in thread Check if the first hash value is present in second hashmap by Anji@BJB

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.