I know I posted something similar earlier but I limited it down. If you can PLEASE try to find the problem with the following code it would be great. For some reason it deletes the entire database file instead of the selected files. Thanks.
sub delete_entries {
&flock($path."/databases/calendar.txt");
open (list, "<$path/databases/calendar.txt") or &error("Unable to open the data file for reading");
@list=<list>;
close(list);
&unflock($path."/databases/calendar.txt");
##############################################
# store all selected addresses into an array #
##############################################
@pairs2 = split(/&/, $buffer);
$count=0;
foreach $pair2 (@pairs2) {
($date,$type,$location,$remarks,$comment1,$comment2,$comment3,$comment4,$comment5) = split(/\|/, $pair2);
if ($field_name eq "event") {
@entries[$count]="$field_value";
$count++;
}
}
#########################################################
# use a foreach loop to delete the selected entry files #
#########################################################
foreach $entry(@entries) {
$count=0;
foreach $list(@list) {
if ($list =~ /$entry/i) {
splice(@list, $count, 1);
print "$entry was removed.
";
}
$count++;
}
}
&flock($path."/databases/calendar.txt");
open (list, ">$path/databases/calendar.txt") or &error("Unable to write to the data file");
print list @list;
close(list);
&unflock($path."/databases/calendar.txt");
exit;
}
sub flock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;
local ($timeout);
$timeout=20;
while (-e $lock_file &&
(stat($lock_file))[9]+$timeout>time)
{ sleep(1);}
open LOCK_FILE, ">$lock_file"
or &error("Unable to create $lock_file");
}
sub unflock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;
close(LOCK_FILE);
unlink($lock_file);
}
sub delete_entries {
&flock($path."/databases/calendar.txt");
open (list, "<$path/databases/calendar.txt") or &error("Unable to open the data file for reading");
@list=<list>;
close(list);
&unflock($path."/databases/calendar.txt");
##############################################
# store all selected addresses into an array #
##############################################
@pairs2 = split(/&/, $buffer);
$count=0;
foreach $pair2 (@pairs2) {
($date,$type,$location,$remarks,$comment1,$comment2,$comment3,$comment4,$comment5) = split(/\|/, $pair2);
if ($field_name eq "event") {
@entries[$count]="$field_value";
$count++;
}
}
#########################################################
# use a foreach loop to delete the selected entry files #
#########################################################
foreach $entry(@entries) {
$count=0;
foreach $list(@list) {
if ($list =~ /$entry/i) {
splice(@list, $count, 1);
print "$entry was removed.
";
}
$count++;
}
}
&flock($path."/databases/calendar.txt");
open (list, ">$path/databases/calendar.txt") or &error("Unable to write to the data file");
print list @list;
close(list);
&unflock($path."/databases/calendar.txt");
exit;
}
sub flock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;
local ($timeout);
$timeout=20;
while (-e $lock_file &&
(stat($lock_file))[9]+$timeout>time)
{ sleep(1);}
open LOCK_FILE, ">$lock_file"
or &error("Unable to create $lock_file");
}
sub unflock
{
$lock_file="$path/databases/calendar.txt";
local ($lock_file) = @_;
close(LOCK_FILE);
unlink($lock_file);
}