Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying flatfile 1

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
I need to remove entries from a flatfile DB that are over 30 days old, but what happens is the whole file gets wiped (definitely not a good thing...)

Any suggestions are greatly appreciated....
Jim

The code I am using is:

Code:
sub delete_old  {
		
	$current_time = time;
		
	$delete_date = $current_time - ($days * 86400);
		
	open (TEMP, ">cards_temp") || &CgiError ("Could not open cards_temp");
	flock (TEMP, 2);
	open (CARD, "$cards") || &CgiError ("Could not open $cards");
	flock (CARD, 2);
	while (<CARD>)        {
			($number, $from, $from_email, $message, $to, $image, $layout, $date_time, $date) = split(/\|/, $_);
			if ($delete_date < $date_time)        {
				print TEMP &quot;$_&quot;;
			}
	}
flock (CARD, 9);
flock (TEMP, 9);
close (TEMP);
close (CARD);
rename (&quot;cards_temp&quot;, &quot;$cards&quot;);
}
 
just to make sure, is the $date_time entry in the database in the same format as a time() return value (should be around a 9 digit number)? &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
well then, there's nothing wrong with the logic of the subroutine. you make access to a variable '$days' that i'm assuming is set somewhere else. you may try rewriting the sub so that it can run under 'use strict;' - predeclare all your variables with my, and pass in the value to be assigned to '$days' as an argument to the subroutine. other than that, the problem is probly elsewhere. try making the 'while (<CARD>)' loop do something like:[tt]
print TEMP &quot;Is $delete_date less than $date_time =>&quot;;
if ($delete_date < $date_time) {print TEMP &quot;Yes\n&quot;}
else {print TEMP &quot;No\n&quot;}
[/tt]
just to make sure that everything is being passed in right. if you try that, comment out the renaming of the database so you won't have to recreate it after each test.

HTH &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
stillflame,

Thanks for the input....

I went with a slight modification of your suggestion, I did comment out the rewrite, then I wrote $delete_date and $date_time to the file for a comparison along with the yes and no as you suggested, everything still looked like it should work.

Then I changed the var $days to $leave_days and went with use strict; and it works perfectly....

Somewhere in the script $days is getting changed or corrupted. I haven't taken the time to tack that down yet, but I wil tackle that next since, if nothing else, I am very curious about where the problem came in.

Once again, thanks for the help.
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top