Flat file

Updated: 12/20/2017 by Computer Hope
flat database

Alternatively called a flat database or text database, a flat file is a data file 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 with all the data needed for a program. These types of files are often separated by a delimiter.

A flat database is easier to understand and set up than a traditional database, but may be inadequate if it 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. Then using the foreach command it goes through each line in the array (file) and splits each line into its own array using the pipe delimiter. After it's 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, Database terms, Programming terms