in reply to Strings/Numbers aren't equating properly

It could be leading or trailing spaces, add TRIM to the sql and try s/\s//g in place of chomp on the @ldap elements. Use Data::Dumper to inspect the values. For example

#!/usr/bin/perl use strict; use DBI; use Data::Dumper; my $dbh = get_dbh(); #as required # get mysql records my $sql = "SELECT TRIM(mid),type FROM table WHERE type != 'NA'"; my $sth = $dbh->prepare($sql); $sth->execute(); my %table = (); while (my ($mid,$type) = $sth->fetchrow_array){ $table{$mid} = $type; } #print Dumper \%table; # check ldap records my @ldap = (" 4\r\n"," 5 "," 6 ",7,8,9,1,2,3); #print Dumper \@ldap; my @new_ldap = (); for my $id (@ldap){ #chomp $id; $id =~ s/\s//g; if (exists $table{$id}){ print "[$id] removed\n"; } else { push @new_ldap,$id; } } #print Dumper \@new_ldap;
update : removed limit of 15 on sql
poj

Replies are listed 'Best First'.
Re^2: Strings/Numbers aren't equating properly
by Anonymous Monk on Feb 22, 2018 at 15:01 UTC
    . . . and if the data is really numeric, check to be sure that they are being handed back as numbers. If they are being handed back as strings, "2.0" and "2.00" are not the same string. Next, bear in mind the usual caveats about making exact comparisons of floating-point numbers, which apply to any programming language.