Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Strings/Numbers aren't equating properly

by poj (Abbot)
on Feb 22, 2018 at 07:58 UTC ( [id://1209720]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209720]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-19 20:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found