[
Database tutorial |
Text databases
]
Deleting records
If you have followed my suggestions about field order within a record, deleting a record from a data file is pretty trivial. This code also assumes that you have some standard field delimiter.
# Assuming that $delimiter contains the field delimiter
# and $id contains the ID number of the record to be deleted
open (FILE, "$datafile");
@data = <FILE>;
close FILE;
@data = grep !/$delimiter$id$/, @data;
# Write it back to the file
# Should probably lock the data file
open (FILE, ">$datafile");
for $record (@data) {
print FILE $record;
}
close FILE;
OK, several comments about this code:
Deleting a record is done by the ID number. This is really the best way to specify records for deletion, especially if you are deleting records one at a time like this. If you are deleting a bunch of records based on some criteria, you can also use grep to do that. But really the whole point of having unique identifiers is so that you don't get the wrong record by mistake when you try to delete or edit a record.
Notice that when I am grepping for an ID number in the record, I am specifying both the delimiter and the end of the record in the match string:
@data = grep !/$delimiter$id$/, @data;
This is so that an ID number will not match a different ID number by mistake. For example, 10 matching 110 or 101, simply because those numbers contain the character sequence '10'.
If for some reason your ID number does not live on the end of your record, there are simple ways around this. You simply have to get a little more creative in your pattern matching in your grep statement.
If, for example, your ID number is the third field in your record, try:
@data = grep !/^.*?$delimiter.*?$delimiter$id$delimiter/, @data;
Notice that this time the ID number is specified sandwiched between two delimiters, to avoid the same sort of false matching mentioned above.
In both the original case and in this modified case, it probably makes sense to put the /o modifier on the end of the grep regular expression for the sake of gaining a few miliseconds per record that you search, and so perhaps a lot of time for the whole file if you have a large datafile. Of course, if you have a large datafile, you should probably be using a real database, but that's another matter.
[
Database tutorial |
Text databases
]