Getting matches that shouldn't be.

J

Jay

I'm trying to write a quick program that outputs a table of compatable
parts for our company. The table is stored in a CSV file. The part I'm
having trouble with is an if statement that seems to be letting things
through when they shouldn't match.

Here is the source code up to the part that isn't working. (It used to
be a little cleaner, but by the end of yesterday I was coding by
flailing around a little).

$compatFileName = "compats.csv";

open(COMPATFILE, $compatFileName) or die "Can't open compatibility
file: $!\n";

#NEED A VARIABLE CALLED $whatProducts that is a comma deliminated list
of products for this page

###################################
##For Debugging
$whatProducts = '104-AI12-8';
#################################


@products = split /,/, $whatProducts;

$compatLine = <COMPATFILE>;
@partNames = split /,/, $compatLine;

foreach $currentProduct (@products)
{
while ($compatLine = <COMPATFILE>)
{
@currentCompat = split /,/, $compatLine;

$count = 0;

$compareTo = join "", split "-", @currentCompat[0];
$currentProduct = join "", split "-", $currentProduct;

chomp ($compareTo);

if ( $currentProduct == $compareTo)
{

print "Checking for: ";
print $compareTo;
print "\tCurrent Product:";
print $currentProduct;
print "\n";


The if statement at the end seems to be letting through anything where
the first three characters match. Here is a few lines from the output:

Checking for: 104AI128 Current Product:104AI128
Checking for: 104AIM32 Current Product:104AI128
Checking for: 104AIM32A Current Product:104AI128
Checking for: 104AIO128 Current Product:104AI128
Checking for: 104AIO1616E Current Product:104AI128
Checking for: 104AIO1616W Current Product:104AI128
Checking for: 104AO124 Current Product:104AI128


I haven't written anything in PERL in a long time, and I was never an
expert at it. If there is a beginner newsgroup I should be posting this
kind of question to please let me know and I'll apologize in advance
for putting this in the wrong place.

-- Jay
 
A

A. Sinan Unur

Here is the source code up to the part that isn't working.

You know, this a big mess to wade through to find the actual problem.
if ( $currentProduct == $compareTo)

Use eq to compare strings instead of ==.

D:\Home\asu1\UseNet\clpmisc> perl -w -e "print 1 if '104a' == '104b'"
Argument "104b" isn't numeric in numeric eq (==) at -e line 1.
Argument "104a" isn't numeric in numeric eq (==) at -e line 1.

If you had enabled your warnings, you would not have needed to embarass
yourself like this.

Please read the posting guidelines. Any code you post here should be
warnings and strict clean to maximize your chances of getting useful
help.
I haven't written anything in PERL in a long time, and I was never an
expert at it. If there is a beginner newsgroup I should be posting
this kind of question to please let me know and I'll apologize in
advance for putting this in the wrong place.

clpm is not a wrong place to post this: You just did not post it the
right way.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top