Flat file

Updated: 04/26/2017 by Computer Hope

flat databaseAlternatively referred to as a flat database or text database, a flat file is a file of data that does not contain links to other files or is a non-relational database. A good example of a flat file is a single text-only file that contains all the data needed for a program that is often separated by some kind of delimiter.

A flat database is much easier to understand and setup than a traditional database, but may be inadequate for any program that is frequently used or contains millions of entries. Below is a basic example of how data in a flat file may appear and be used in a Perl program.

Flat file example

Bob|123 street|California|$200.00
Nathan|800 Street|Utah|$10.00

Perl script to read flat file

use strict;
my (@users, $users, @display, $display);

open (EXAMPLE, "<flatfile.txt") || die;
@users = <EXAMPLE>;
close(EXAMPLE);

foreach $users (@users) {
chomp($users);
@display = split(/\|/, $users);
print "$display[0]\n$display[1]\n$display[2]\n\nHello $display[0],\n\nYou currently owe us $display[3], please pay us as soon as possible.\n";
}

In the above example, the Perl script first opens the flatfile.txt and places the data into any array. The script then uses the foreach command to go through each line in the array (file) and as it goes through each line splits each line into its own array using the pipe delimiter. After it has been loaded into its own array each segment of the array can be called, for example "$display[0]" is the first element of the array. So for the first line the script would print the below message.

Bob
123 street
California

Hello Bob,

You currently owe us $200.00, please pay us as soon as possible.

CSV, Database, Programming terms