foreach my $uid (keys %results){ $search = $ad_ldap->search( base => 'DC=company,DC=domain,DC=lan', scope => 'sub', filter => "(&(cn=".$uid.")(objectclass=user))", attrs => ['lastlogon', 'cn'] ); # if no results were returned, set ad_lastlogintime to 0 and go to next uid if ( $search->count == 0){ $results{$uid}{ad_lastlogintime}{0} = 1; next; } # store the AD last login time in the uid hash foreach my $entry ($search->entries){ $results{$uid}{ad_lastlogintime}{$entry->get_value('lastlogon')} = 1; } }