Edit: This is fixed. It was my fault...Flawed logic, it took time to work out. Explanation at the bottom of this post.
I'm trying to compare results from an LDAP server to what lives on a MySQL server. Overall it works, right now I'm trying to optimize a part of it. I took the results of an LDAP query returning a single string value to an array '@ldap' which I then sorted alphabetically.
Now I'm doing a search in MySQL, and as I iterate through the results, I'm comparing one column to the '@ldap' array and then I remove any matches out of '@ldap'. The problem is that sometimes, even though strings appear identical, it doesn't count it as a match and so fails to remove the match out of '@ldap', which creates redundant work at the end of the script.
Here's the problematic code:$sth = $dbh->prepare("select * from table order by mid"); # So +rting alphabetically $sth->execute(); while(my $info = $sth->fetchrow_hashref){ my $count = 0; my $mid = $$info{'mid'}; chomp($mid); my $num = 0; if ($mid =~ /^[0-9]+$/){ # Checks to see if string hap +pens to be only numbers print fh "$mid is a number\n"; $num = 1; # If so, change value } else { print fh "$mid is a string\n"; # Prints to a t +ext file for me to look at. } print "$mid - \$num = $num\n" if ($debug > 0); foreach my $id (@ldap){ chomp($id); # Check to see if we've found a correlating LD +AP entry from MySQL if ($num == 1){ print fh "$mid - $id - Equating as numbers.\n"; last if ($count > 15); if ($mid == $id){ # Check type to see if we have + data on it if ($$info{'type'} ne 'NA'){ # If we do, then remov +e this ID from the list to retry splice @ldap, $count, +1; last; } } } else { print fh "$mid - $id - Equating as strings.\n"; last if ($count > 15); # To drastically shorten during troubleshooting if ($mid eq $id){ # Check type to see if we have + data on it if ($$info{'type'} ne 'NA'){ # If we do, then remov +e this ID from the list to retry splice @ldap, $count, +1; last; } } } $count++; } } $sth->finish;
At first I thought it was because sometimes the strings contain only numbers, so I decided to check if they do contain only numbers, and if so, compare using '==' instead of 'eq'. I also was expecting to be able to use just $$info{'mid'} directly for comparison, so I did my $mid = $$info{'mid'}; for the sake of troubleshooting.
In the file output, regardless of if they equate as strings or numbers, sometimes it just doesn't match when it looks like it should. Now I'm stumped. I'd be grateful for help, thank you!
Edit: It's working now. My flawed logic was here:
if ($$info{'type'} ne 'NA'){ # If we do, then remove this ID from the list to retry splice @ldap, $count, 1; last; }
I ended up having a lot more entries of type 'NA' than I was expecting, and due to work later in the script it ended up making more sense to put those entries into another array to separate out the work. So I changed the logic to this:
I appreciate everyone's time, thank you.if ($$info{'type'} eq 'NA'){ push(@file, $id); } # If we do, then remove this UID from the list to retry splice @ldap, $count, 1; last;
In reply to Strings/Numbers aren't equating properly by M4ver1k
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |