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!

unlink single line of data

Status
Not open for further replies.

envar

Technical User
Jan 5, 2002
2
US
This is a project that has been back-burner'd - and now I'm faced with adding an unlink (delete record) routine.

Data is stored in a flat file database using an 'old' style Perl cgi. Each record consists of many fields. Each record is written to two separate databases - one db per category (category.db) and one that stores all category's records (big.db). Each db has anywhere from 50 to 100 record lines.

a record line may look like:

state:NM|food:chili|color:blue, purple, red green|etc:etc| (lots of fields that end in a pipe)

Using a form to select which record to edit (delete) - how do I unlink that "line" from the category.db and big.db? I don't know how to get all those fields into an array and unlink.

I can unlink a particular file easy enough - but I've never had to unlink a line from a data file, let alone two different data files at the same time.

Sorry, but I really need some pointers on this. It's down to the wire with this project.
 
Something like:

open FILE, '/path/db';
@slurp = <FILE>;
close FILE;
open FILE, '>/path/db';
foreach (@slurp)
{
next if #(condition to find line you want removed);
print FILE;
}
close FILE;

This will take all the lines from your database file and write them back out unless the line matches some criteria (By way of some regex). I would suggest writting to a test file until you're satisfied your script works.


 
Or use the splice() function. Yauncin's method above is actually a pretty good one, except that with a large file your using alot of memory, another way to do it would be:

open INFILE, '/path/db';
open OUTFILE, '/path/newdb';

while <FILE> {
print OUTFILE unless #insert matching pattern here;
}

close(INFILE);
close(OUTFILE);

rename '/path/db','path/olddb';
rename '/path/newdb','/path/db';

This method saves on memory a bit, and gives you a backup file.

Hope this helps

--Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top