
This article is reprinted from the February 1991 edition of TechNotes/dBASE
IV.  Due to the limitations of this media, certain graphic elements such as
screen shots, illustrations and some tables have been omitted.  Where
possible, reference to such items has been deleted.  As a result,
continuity may be compromised.  

TechNotes is a monthly publication from the Ashton-Tate Software Support
Center.  For subscription information, call 800-545-9364.

What Is  an Array?

Joe Stolz

The question used to be "Why doesn't dBASE use arrays?"
Now that dBASE IV supports them, the question is "What are they?"

Your average dictionary would define "array" as a splendid assortment, sort
of like your average box of Whitman's.  But in the computer language world,
arrays, somewhat like their chocolate counterparts, are an ordered group of
values kept in memory under the banner of a single name.  Arrays are very
useful if you know how to apply their usage.  If you have no idea what they
can be used for, it's just another page in the manual with techno-babble
that you have to turn through.  So, let us try to offer a simple view of
arrays and some solid (as opposed to soft-centered) examples of how they
can be used.  In the end they should appear to be useful additions to the
dBASE IV language.

Creating Arrays

As we said, an array is a list of items, kept in memory, that allows you to
reference the list in a simple logical fashion.  An array has a name and is
made up of a prearranged number of items.  You create an array somewhat
differently than you create memory variables.  Memory variables can be
created by assigning them a value.  They can also be created through the
less known PUBLIC or PRIVATE statements.  You can create the memory
variable m_sum by declaring it as a memory variable that is PRIVATE to the
program in which it is declared.  That is, you have the line:

PRIVATE m_sum 

on a line by itself in your program.  This creates a memory variable that
really has no specific value assigned to it.  In fact, all variables that
are created via PRIVATE or PUBLIC statements have a logical false (.F.)
value assigned to them initially.  When you explicitly give the variable a
value is when it can take on more than a simple boolean value.

The simplest and most common way to create variables is to directly assign
them a value.  That is:

m_sum = 0

both creates the new variable and also gives it a value of zero.

Arrays, however, must always be created in two steps: a declaration and
then an assignment.  This is done as follows:

DECLARE 3_values[3]

This creates an empty array of three items or elements and leaves them
unassigned.  As in the memory variable case, the 3 elements all have a .F.
value by default.

Actually, these three array elements appear as if they were three separate
memory variables when you look at them by typing DISPLAY MEMORY.  They show
up as
 3_ values[1], 3_values[2] and 3_values[3].  You assign new values to these
array elements by direct assignment as if they were regular memory
variables except that you must indicate the proper "index" of the element
being assigned.  An index is the number in the square brackets which
indicates which element is which in the list of elements that make up the
array.  

Using Arrays

Let's discuss a common usage for arrays.  By now, a dBASE IV user knows the
difference between fields and records in a database.  The fields in the
file are a series of data elements that make up a record.  There may be 10
or 100 fields in each record.  A series of repeating records make up a file
or database.  An array can therefore be a memory based representative of a
record.  If your database has 12 fields in it, you can represent a record
by first declaring an array of 12 elements, then assigning values to each
of the elements in field order.  Here's an example:

DECLARE Curr Name[12]                           && 12 fields per record

Curr Name[1] = "John"                           && equivalent to Firstname
Curr Name[2] = "Wadsworth"              && Lastname 
Curr Name[3] = "122 So. Main"           && Address
Curr Name[4] = "Los Angeles"            && City 
.
.
.

This way you can have a series of elements in memory that represent a
record in your database.  You can use your array as a record to enter and
validate data without even opening the database.  This raises a couple of
questions: What is the difference between 12 regular memory variables and
this 12 member array?  Just what does an array offer over memory
variables?  Well, dBASE IV has gone out of its way to offer several
commands that capitalize on your choice to use arrays.  First, now that you
have an array that simulates your record structure, you can use the new
dBASE IV command REPLACE FROM ARRAY which can, in one shot, copy the data
from your array into the appropriate matching fields in your file.  REPLACE
FROM ARRAY can be used to modify one or many records.  This allows you to
reference a single array name in this operation instead of 12 different
variable names.

Other commands that are related to arrays are COPY TO ARRAY and APPEND FROM
ARRAY.  These copy one or more records into a pre-DECLAREd array and add
one or more records from an array onto the bottom of a file, respectively.

Beyond a Single Dimension

The power of such an array as we've just described is welcome but one
quickly sees the limitations of a one-dimensional array.  It should then be
comforting to know that dBASE IV supports two dimensional arrays as well. 
This type of array follows the memory based database analogy even more
closely. A database is a series of fields that repeats its pattern over
many records.  One "dimension" is the number of fields in each record.  The
"second dimension" is the number of records contained in your file.  To
represent a file of 50 records where a record is made up of 12 fields you
could declare an array like this:

DECLARE Many Recs[50,12]

where the first number in square brackets represent rows and the second
represents columns.  Many_Recs is an array where each record has 12 rows
and there are 50 records in the full array.  Consequently, the array
consists of 50 * 12 or 600 elements.  

In dBASE IV, if the array being used is two dimensional, the APPEND FROM
ARRAY and REPLACE FROM ARRAY commands will apply sequentially to each
element in the array.  If the array is one dimensional (columns only) and
the REPLACE FROM ARRAY command has a FOR or WHILE condition, when the
condition is met, the same values will be used every time a REPLACE is
made.

It is interesting to note that in most other programming languages, there
is array support, but that arrays are quite often used for storing a long
series of numbers or elements, then processing that series of elements
sequentially.  For example, you could create an array that contains as many
elements as you have records in your file.  Afterwards, copy a value from
each record into separate elements of the array.  You could then process
the array sequentially and produce,  say,  an average.  (This seems like an
awful lot of work to produce an average when dBASE IV provides an AVERAGE
command that works directly on your file, but it helps to underscore the
fact that a database is, in a sense, a two-dimensional array on disk.)

In earlier versions of dBASE,  "pseudo-arrays" could be created by
employing "& macros".   By attaching a trimmed, converted string of an
incrementing number to a literal (such as the word "TABLE", for example),
you could come with functionality close to that of storing indexed values. 
But the process of getting that information to and from records became
quite cumbersome.  Although it may have been a clever workaround in its
day, the process required some creative coding on the part of a new
programmer and not the easiest type of code to maintain. 

Now, in dBASE IV, a DO loop can variably reference an index in an array in
a sequential manner.  The basic idea involved in processing a series of
array elements is:

DECLARE Array1[100]
* assign values to the array
x= 1
DO WHILE x < 101
                some var = Array[x]
                * do some processing here
                x = x + 1
ENDDO

Here, as x increases from 1 to 100, we can capture the value of each array
element one by one and perform some process.  The main advantage of an
array here is that it offers an orderly means of addressing a series of
memory variables in a sequential fashion.  Memory variables that do not
contain an "index" require difficult gyrations in order to address them
sequentially.

These are the basics of arrays.  There are only a few rules involved in
dealing with arrays but none that should dissuade you from using them.  The
only valid limitation is that of memory.  Large arrays may cause
insufficient memory conditions during their declaration if not enough
memory is available.  Otherwise, the ability to COPY TO , APPEND FROM or
REPLACE FROM an array to and from records is a welcome addition to dBASE
IV. These commands give you a simple means of addressing a sequence of
data, or as a temporary place to put one or many records from your file. 

