Gossamer Forum
Home : General : Perl Programming :

Question

Quote Reply
Question
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);
}


Quote Reply
Re: Question In reply to
Code:
open (list, ">$path/databases/calendar.txt") or &error("Unable to write to the data file");
You're opening the file for writing, not appending. That will clobber the whole file.

Quote Reply
Re: Question In reply to
What the script does is open it, grab the files and make them into an array, deletes the lines from the array that are selected, and then rewrites the file do you see what might be wrong then?

Quote Reply
Re: Question In reply to
Hi mike101,

As Mark pointed out, you are opening the file to write, not read. Change the > to < to read your file in first.

Regards,
Charlie

Quote Reply
Re: Question In reply to
Hi,

If you could provide more information such as the database format and explain exactly what you are trying to do then I'm sure someone can come up with the appropriate coding.


I realise you have explained a bit already but pasting large chunks of code in a post isn't always as helpful as you'd think.

If you could provide the steps that are occuring, such as

"User enters something"
"Database is scanned for something"
"If matched, remove something"

....then that would be of great help as we can write the code for you for each step.



Installations:http://www.wiredon.net/gt/

Quote Reply
Re: Question In reply to
It does read it correctly.

&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");

is where it reads from the flat file database it has the right < thing.

Quote Reply
Re: Question In reply to
I'm not sure where you got those flock and unflock routines, but yikes.

Anyways, that's where your problem is, your unflock routine is unlink'ing the database file you created. I think you want:

&flock ($path . '/databases/calendar.txt.lock');

and

&unflock ($path . '/databases/calendar.txt.lock');

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Question In reply to
Could you describe a little further because I took this from another script I found on the internet.

Quote Reply
Re: Question In reply to
I'd remove the &flock and &unlock routines altogether and just use:

Code:
flock(list, 2);
...just under open(list...



Installations:http://www.wiredon.net/gt/