You are here: Automating Analyzer > Variables > Variable Arrays

Variable Arrays

Analyzer supports using variables to store an array of values, in either numeric, character, datetime or logical formats.

Note: Variable arrays are also valid for use as an alternate to specifying a list in a Dialog command, for example in a drop-down list. For more information see Manually Editing Dialog Commands and Variables.

A variable array must use a consistent data type for all array elements; mixing of numeric, character, datetime or logical values is not permitted within the same variable array. Additionally, the elements in the variable array must have the same format/characteristics. For example, each numeric value must have the same number of decimals and each character string the same (maximum) string length.

Note: Variable arrays are technically infinite in size so referencing the value of an array element that does not yet exist will return an empty string, 0, an invalid date (January 1st, 1900) or false, as appropriate.

Creating Variable Arrays using the Assign command

The array type (numeric, character, date or logical) and value is set by the first assignment to the variable. For example, using the Assign command:

assign x[100]=0.00

assign y[100]=blanks(100)

would create a variable array called x as an array of 100 numbers with 2 decimals and would create a variable array y as an array of 100 strings with a maximum length of 100 characters.

Tip: Processing variable arrays is always most efficient when the maximum array size is established up front.

The easiest way to establish the maximum array size is to initially assign a default format and data type to the largest element number in the variable array (as shown in the previous example). Establishing a new array will also assign default values to all unspecified elements, for example:

a[3]=`20130814` results in a[1] and a[2] having empty date values
b[3]=51.12 results in b[1] and b[2] having the numeric value 0.00
c[3]=T results in c[1] and c[2] having the logical value F

Note: There is presently a maximum array size of 1,000,000 elements when using the Assign command to create the variable array. This limitation does not apply to the creation of variable arrays using the SAVE FIELD command.

If subsequent variable assignments to array elements are made that don't match the previous definition then the result is forced to match (if possible) and the standard "adjusting" message used for variable assignments within the Group command is displayed in the Command Log. For this reason it is always best to get the initial variable array format and data type assignment correct the first time the variable array is initialized.

Note: Assigning an invalid value to an element in a variable array will cause the value to default to either an empty string, 0, an invalid date (January 1st, 1900) or false, as appropriate.

For example, assigning a shorter string will not generate a warning (the string will simply be padded with blanks), but assigning a string longer than the maximum will generate a message to the Command Log and the string value will be truncated. Similarly, assigning a numeric value with fewer decimals will simply result in the number of decimals being increased without warning, while assigning a numeric value with a larger number of decimals will result in the decimals being rounded down with warning message in the Command Log.

Creating Variable Arrays using the Save Field command

When the items you wish to use to populate a drop-down list are stored within a field (column) in an existing table, an easy automated way to create a variable array containing a list of these items is to open the table and then use the SAVE FIELD command to save the field data into a named variable array. For more detailed information see Save.

Referencing Variable Array Elements

The basic syntax for referencing an array element in a variable array x is:

x[n]

where n=1 is the first entry in variable array x. If you refer to an array variable without using the element subscript, you will always return the value of the of the first element in the array

Array element subscripts are always a positive whole number starting from 1. When specified, the element subscript can be a literal (1,2,3, etc.) or can be a variable. When using a variable, the value can be incremented or decremented as needed, typically in a procedure using Group and Loop commands. For example:

x[1]

x[n]

x[n+1]

Variable arrays are virtual and therefore provide very space efficient storage for large amounts of temporary data, as only the data is duplicated, not the definition.

Tip: While array entries can be created/updated in any order, from an efficiency point of View, it is best to assign the highest array element value first (even if just to initialize to a zero value) as any re-sizing of the array requires additional processing time.

Syntactically, array variable element subscripts can be used anywhere a traditional variable can be used, EXCEPT in macro substitutions.

For example:

EXTRACT x[3] y[4]

If you use an array variable in a macro (without the subscript [ ]), then you will get the value in the 1st element in the array, as described above.

Deleting or Resetting Variable Array Elements

You cannot delete an array element, it can only be reset to a default value (zero, blank, etc.):

x[3]=0.00

y[4]=Blanks(10)

A variable array can be completely deleted:

Delete x OK

Delete y OK

Examples

When the following value is entered:

x[7]="abc"

it creates a 7 element array of strings (maximum length is 3), where the 7th entry value is "abc". All other elements have a default value of 3 blanks.

When the following value is entered:

n[5]=0.00

it creates a 5 element array of numeric values, all containing values of zero with 2 decimals.

When the following value is entered after the example above:

n[10]=1.0

it will increase the array to size from 5 to 10, generate a warning message in the Command Log and create a 10th element whose value is 1.00

When the following value is entered:

x[n]=x[n]+1

it will increment the n'th element of the x variable array. In a Group command, the value of n many change by record.

When the following value is entered:

y=n[3]+n[4]

it will add the third and fourth entries of the variable array n together and place the resulting value in variable y.

Finally, the following example shows how to use variable arrays in a Group by assigning the variable attributes outside the Group and then assigning the value of each variable element on a record to record basis (accumulating array element totals as required).

Open Department_Table

Sort On Department_Code to Sorted_Department_Table

Open Sorted_Department_Table

 

COMMENT Create a variable array of 100 elements,

COMMENT one for each department code,

COMMENT each with default value of zero

 

Dept_Amount[100]=0.00

 

COMMENT Use a Group to accumulate the department totals,

COMMENT assigning each department total to an array element

 

Group

Dept=Value(Department_Code,0)

Dept_Amount[Dept]= Dept_Amount[Dept]+Amount

End