Helpful Information
 
 
Category: Perl Programming
Please Help with this SUB

Hello everyone,

I have a sub (below) that opens and retrieves certain data from another. The problem is when it prints to the new file....

How would i stop it adding duplicate entries??

------ code ------
sub build_email_db {
# --------------------------------------------------------
# Backs up important database files.
print "<b>Backing up Email Databases . . .</b>n";
open (DBIN,"<$aps_orders_db");
open (DBOUT,">>$aps_mail_db");

while (<DBIN> )
{@in=split(/|/,$_);

&parseemail;
if ($existing ne $test) {
print "t$in[2] added to Customer Email Database... debug...</b>n";
print DBOUT "$in[1]|$in[2]n";

}
else {
print "t$in[2] is already in the Customer Email Database..(debug half working)</b>n";
close(DBIN);
close(DBOUT);
}
if ($LOCK_EX){
flock(DBIN, $LOCK_EX); #Locks the file
flock(DBOUT, $LOCK_EX); #Locks the file
}
@database_array = <DBOUT>;

foreach $lines(@database_array) {
chomp($lines);


# if ($lines =~ /$INPUT{'email'}/i) {

}
}

------ end ------

open DB
read DB and assign it to array
close DB
foreach line at the array # the loop
open BACKUP # read-only
if line exists
next
close BACKUP
else
open BACKUP again
write the line to it
close BACKUP
# end loop

You don't need to lock any file at all, you are the only one who is doing the backup. As for the DB file, it will read thru EOF, don't need to lock it as well.










privacy (GDPR)