[ Database tutorial | Text databases ]

Data record delimiters

When you are putting records in a text file, you need some way to indicate that one field is finished and the next is starting. This is called a "delimiter." One fairly common delimiter is a comma. For example, one record in your data file might look like:

Bowen,Rich,rbowen@rcbowen.com,CGI Programmer
The commas indicate the different fields. The problem occurs if one of the fields might contain a comma. For example, suppose my title (the last field) was "Ruler of Space, Time, and Dimension." There would be no way to know if those commas were part of the field, or if they indicated a break between fields.

And, so, when using text data files, it is important to choose your delimiters in such a way that they are never going to be part of any field, to cause this sort of confusion.
Since it is always possible that a particlar character might show up in a field, I recommend using multi-character delimiters, such as ### or %%% or ~~~, which are extremely unlikely to ever be a part of one of my data fields.

Writing and reading data to/from these files is then simply a matter of knowing what your delimiter is, and what order the fields are to be in.

Sample Code - Writing a new record

$delimiter = "%%%";

$new_record = join $delimiter, $field1, $field2, $field3, $field4, $field5;
print DATAFILE "$new_record\n";

Sample Code - Reading a record

open (DATA, "$datafile");
$record = <DATA>;

($field1, $field2, $field3, $field4, $field5) = split /$delimiter/, $record;

[ Database tutorial | Text databases ]