Home | Web Design | Programming | Fairlight CMI | Soap Box | Downloads | Links | Biography | About... | Site Map |
Understanding Arrays |
CA-Clipper | Opportunities | Tips and Tricks | Networking | Internal Errors | Source Code | CA-VO |
BACK TO CA-CLIPPER TIPS & TRICKS |
An array is a collection of values. Or, put another way, it is a list.
Or a bag full of things. An array can be created in a number of ways. The Initializing Arrays page shows some of them. Each item in the array is referenced by indicating its numeric position in the list, starting from item number 1. The following code declares a variable, assigns a three-element array to it, then displays one of the elements and the array's length.
aadd() function.
aadd() adds an element to the end of the array. There are three other common array functions.
adel() and ains() functions are interesting
in that they both do not change the length of the array. The
designers of CA-Clipper chose this behaviour to give you more flexibilty.
They left it to you to write functions that do change the array
size, as in the following samples:
|
Arrays as Structures
A very nice feature of CA-Clipper is that an array can hold anything: numbers, dates, logicals, strings, etc. All at the same time. In other words, the elements of the array need not all be of the same data type, in contrast to C and Pascal. aPerson1 := {"Bob", 32, .T.} This array contains a string, a number, and a logical value. In other languages (such as C or Pascal) this "package" of information might be called a "structure" (in C) or "record" (in Pascal). The word "record" should bring to mind an actual database record that would be stored in a DBF file. A DBF record is a package of information which is made up of several fields. Each field can have a different piece of data in it. Many records (each with the same layout of fields) are collected together in a DBF file. Suppose that the aPerson1 array contains information about a
person's name, age, and marital status. The following #defines
can be created to indicate each value's position in the array:
PERSON_NAME constant is not strictly necessary
(it simply replaces the number 1), but it adds a good bit of readability
to the code. Now, instead of working with the individual local variables, why not gather them up into another array, in the same way that many records are gathered up into a DBF file.
aPeople array looks like a two-dimensional matrix with 3
rows and 3 columns. This way of thinking can be helpful when starting to
work with arrays, but can fall apart for more complex structures that do
not have a nice square layout. Once the separate variables have been combined into an array, the names can be printed like this:
nL variable selects which person (or row) is of interest.
Then the PERSON_NAME constant selects the first column of
that row. Or, using CA-Clipper's aeval() function (which is basically
a for...next loop): aeval( aPeople, {|e| qout( e[PERSON_NAME] } ) That example uses the qout() function which is equivalent to
the ? command. The function is contained in a code block. For
more about code blocks, see
The Evolution of Code Blocks. You could add 10 years to everyone's age like this:
|
Uses for Arrays
Another common technique is to use a two-dimensional array (similar the aPeople array above) to represent the main menu bar across
the top of the screen and the drop-down menus. Or, a multi-dimensional array could store the report totals (as they are being accumulated) for each year, region, manager, salesman, and product. That's 5 dimensions. Also, all input screens that use the get command manipulate
an array of get objects called GetList . Objects
themselves are kind of like "smart" arrays -- self-contained packages of
related information and functions. So the GetList variable
is a kind of two-dimensional array. Another example is the @...prompt command which builds
up an array of menu items. The menu to... command
manipulates the array. You could say "arrays are everywhere in CA-Clipper", but that is not really the case. Use the right tool for the job. For example, many systems store picklists in DBF files (such as part numbers, client codes, etc.), so there is no reason to put the information in an array -- just use the dbedit() function or the
tbrowse class directly on the table. Also, many programmers store menus in DBF files as well. Once that is done, it becomes quite easy to add a field to the table to give each menu item an access level or "rights mask" to control what users are allowed to do. In addition, translating the menu into another language is fairly straightforward. Arrays can only hold 4096 elements, whereas DBF files can contain millions of records. However, each element of an array can be another array (making it a two-dimensional array), which could give 4096x4096 elements in total. That's 16,777,216 elements. If each element takes up a few bytes of memory you can see that you could run out of memory pretty fast (see the sidebar How much memory does an array need? for details). You can create two-dimensional, three-dimensional, four-dimensional -- all the way to n-dimensional arrays, where n is infinity, subject to the amount of available memory. In other words, arrays can have any number of dimensions, but each dimension is limited to 4096 elements. |
Home | Web Design | Programming | Fairlight CMI | Soap Box | Downloads | Links | Biography | About... | Site Map |
Send comments about this site to Greg at
gregh@ghservices.com All pages copyright © 1996-1999 GH Services Created 1997/11/18 Last updated 1999/09/30 All trademarks contained herein are the property of their respective owners |