Gossamer Forum
Home : General : Perl Programming :

Chomp or chop?

Quote Reply
Chomp or chop?
$job = "$FORM{'job'}";

sub check_no {
open(DB,
"/home/check.db") || die "Unable to open. Reason: $!";
foreach $line (<DB>) {
chomp;
if ($line =~
/$job/) {
&error(job_already_exists);
}
else {
&check_job
}
}
}
#########################################

sub check_job {
$exists = 0;
open(TXT,
"/home/check.db") || die "Unable to open. Reason: $!";
foreach $line (<TXT>) {
chomp;
if ($line =~
/$job/) { $exists = 1; last;}
}
close(TXT);


if (!$exists) {

open(TXT,
">>/home/check.db") || die "Cant open file. Reason: $!";
print TXT
"$job\n" ;
close (TXT) ;
}
}

###################################

If I enter the job #: 000789, it accepts it without any error and write it to the check.db and the next time if someone tries to add the same job no i.e 000789 to DB it will give an error(job_already_exists). Its fine till here its according to script

Now the problem is: its also giving error on:

00078

0007

000

00

0

Its giving an error to all above job no. &error(job_already_exists). Even I didn't enter any of the above job nos. except for 000789. Where I m wrong please help?
Quote Reply
Re: [zeshan] Chomp or chop? In reply to
Im not really sure how this relates to your thread subject line but anyway...

I think the problem relates to the fact that your regex is:

if ($line =~ /$job/) {

....you are just testing if $line contains $job and so if the job id is:

000123

...and you enter 0 then the match is true. It is also true for 00, 000, 0001, 00012, 000123 so you'll want to split on your database delimiter and then use:

if ($split_field == $job) {

...to do your match.

Last edited by:

Paul: Mar 25, 2003, 4:49 PM