Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

The Holmes Page Initializing Arrays

CA-Clipper Opportunities Tips and Tricks Networking Internal Errors Source Code CA-VO



BACK TO
CA-CLIPPER
TIPS & TRICKS

The array is a very powerful tool in CA-Clipper. It can be used to store many different values. It is commonly used to store a collection of related values that will be presented to the user in a picklist. 
Sometimes you know ahead of time (that is, when you are writing the source code) how large an array is. At other times the size of the array is not known until run-time. 
If array size is known


If the array size is known at the time the program is written, there are several ways to implement the code.
01  local aX[10]
02  local aY:=array(10)
03  local aZ:={0,0,0,0,0,0,0,0,0,0}
04
05  for nL := 1 to 10
06     aX[nL] := nL * nL
07  next nL
This code fills the array with a table of squares. The values will be 1, 4, 9, 16 ... 81, 100. Note that line 6 refers to the aX variable, but it would also work if the aY or aZ variables were used. The point of this sample is to show three ways of creating an array if the size is known at the time of writing the code. 
On line 1 the array is created using aX[10]. This causes CA-Clipper to allocate space for 10 elements. The square brackets [ and ] are used to indicate the size required. 
Line 2 uses the array() function with a parameter of 10 to create the array, then the return value of the function is assigned to the aY variable. 
Line 3 does what I like to call "painting a picture of the array". As you can see, there are ten 0's in the list surrounded by braces (sometimes called "squiggley brackets" or "curley braces"). Clearly, you would not use this method to create an array containing 1000 elements! 
The third method differs from the first two because it initializes the array with actual values. In the first two methods, each element of the array contains a value of NIL, which is an empty placeholder. 
Line 6 shows that a value can be assigned to an already existing array element by specifying the index, or "slot number", using the square brackets. 
If array size is unknown


If the array size is not known until run-time, there are several ways to create an array and then add elements to it. The sample below illustrates the idea of creating an empty array (one with no elements) and adding to it dynamically.
01  local aX[0]
02  local aY:=array(0)
03  local aZ:={}
04
05  for nL := 1 to nSize
06     aadd(aX, nL * nL)
07  next nL
Line 1 uses the square brackets to create an empty array. Although the array has no elements, it still has an array "feel" to it. 
Line 2 calls the array() function which creates an array with no elements. 
Line 3 is my choice for representing an empty array in source code. Once again, it uses the curley braces to paint a picture of the array. Note that {} is an empty array (the array has a length of 0), whereas {NIL} is an array with one empty element (the array has a length of 1). 
Because each of these arrays start with no elements, line 6 uses the aadd() function to add elements successively to the end of the array.


Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

Site Map Send comments about this site to Greg at gregh@ghservices.com
All pages copyright © 1996-1999 GH Services™   Created 1997/06/06   Last updated 1999/09/30
All trademarks contained herein are the property of their respective owners