Using Arrays
- Last UpdatedJul 18, 2023
- 3 minute read
A Cicode variable array is a collection of Cicode variables of the same data type, in the form of a list or table. You name and declare an array of variables in the same way as any other Cicode variable. You can then refer to each element in the array by the same variable name, with a number (index) to indicate its position in the array. Working with arrays involves the following:
Declaring Array Properties
Arrays have several properties that you need to declare to the compiler along with the array name: data type, size and dimension. You can also set default values for individual elements of the array. An array declaration has the following syntax:
DataType Name[Dim1Size,{Dim2Size},{Dim3Size}]{=Values};
Declaring the Array Data Type
As with any other Cicode variable, arrays can have four Data Types:
|
INT |
Integer (32 bits) |
|
REAL |
Floating point (64 bits) |
|
STRING |
Text string (255 bytes) |
|
OBJECT |
ActiveX object (32 bits) |
|
QUALITY |
Plant SCADA Quality (64 bits) |
|
TIMESTAMP |
Date and Time (64 bits) |
Naming Arrays
Throughout the body of a Cicode function, a Cicode variable array is referred to by its name, and individual elements of an array are referred to by their index. The index of the first element of an array is 0 (that is a four element array has the indices 0,1,2, and 3). You can name a variable any valid name except for a Restricted Cicode Keywords; for example:
STRING StrArray[5]; ! list
REAL Result[5][2]; ! 2-D table
INT IntArray[4][3][2]; ! 3-D table
Declaring the Variable Array Size
You need to declare the size of the array (the number of elements the array contains, from 1 to 32,767), for example:
STRING StrArray[5];
This single dimension array contains 5 elements. The compiler multiplies the number of elements in the array by the size of each element (dependent upon the Data Type), and allocates storage for the array in consecutive memory locations.
You cannot declare arrays local to a function. However, they can be declared as Module (that is at the beginning of the Cicode file), or Global. When referring to the array within your function, take to care to remain within the size you set when you declared the array. The example below would cause an error:
STRING StrArray[5];
...
StrArray[10] = 100;
...
The compiler allows storage for 5 strings. By assigning a value to a 10th element, you cause a value to be stored outside the limits of the array, and you could overwrite another value stored in memory.
Setting Default (Initial) Array Values
When you declare an array, you can (optionally) set the individual elements to an initial (or start-up) value within the original declaration statement. For instance, naming a string array "ArrayA", sizing it to hold 5 elements, and initializing the array with string values, would look like the following example:
STRING ArrayA[5]="This","is","a","String","Array";
This array structure would contain the following values:
ArrayA[0]="This"
ArrayA[1]="is"
ArrayA[2]="a"
ArrayA[3]="String"
ArrayA[4]="Array"
Passing Array Elements as Function Arguments
To pass a Cicode variable array element to a Cicode function, you need to provide the element's address; for example:
/* Pass the first element of ArrayA. */
MyFunction (ArrayA[0])
/* Pass the second element of ArrayA. */
MyFunction (ArrayA[1])
/* Pass the fifth element of ArrayA. */
MyFunction (ArrayA[4])
Using One-dimensional Arrays
To use a one-dimensional array:
STRING ArrayA[5]="This","is","a","String","Array";
This array sets the following values:
ArrayA[0]="This"
ArrayA[1]="is"
ArrayA[2]="a"
ArrayA[3]="String"
ArrayA[4]="Array"
Using Two-dimensional Arrays
To use a two-dimensional array:
REAL ArrayA[5][2]=1,2,3,4,5,6,7,8.3,9.04,10.178;
This array sets the following values:
|
ArrayA[0][0]=1 |
ArrayA[0][1]=2 |
|
ArrayA[1][0]=3 |
ArrayA[1][1]=4 |
|
ArrayA[2][0]=5 |
ArrayA[2][1]=6 |
|
ArrayA[3][0]=7 |
ArrayA[3][1]=8.3 |
|
ArrayA[4][0]=9.04 |
ArrayA[4][1]=10.178 |
Using Three-dimensional Arrays
To use a three-dimensional array:
INT ArrayA[4][3][2]=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24;
This array sets the following values:
|
ArrayA[0][0][0]=1 |
ArrayA[0][0][1]=2 |
ArrayA[0][1][0]=3 |
|
ArrayA[0][1][1]=4 |
ArrayA[0][2][0]=5 |
ArrayA[0][2][1]=6 |
|
ArrayA[1][0][0]=7 |
ArrayA[1][0][1]=8 |
ArrayA[1][1][0]=9 |
|
ArrayA[1][1][1]=10 |
ArrayA[1][2][0]=11 |
ArrayA[1][2][1]=12 |
|
ArrayA[2][0][0]=13 |
ArrayA[2][0][1]=14 |
ArrayA[2][1][0]=15 |
|
ArrayA[2][1][1]=16 |
ArrayA[2][2][0]=17 |
ArrayA[2][2][1]=18 |
|
ArrayA[3][0][0]=19 |
ArrayA[3][0][1]=20 |
ArrayA[3][1][0]=21 |
|
ArrayA[3][1][1]=22 |
ArrayA[3][2][0]=23 |
ArrayA[3][2][1]=24 |
You use arrays in your functions in the same way as other variables, but arrays have special properties that, in many situations, reduce the amount of code you need to write.
Using Array Elements in Loops
You can set up loops that deal efficiently with arrays by incrementing the index number. The following example shows a method of initializing an array:
REAL Array[10]
:
FOR Counter = 0 TO 9 DO
Array[Counter] = 0
END
RETURN Total
:
Using the Table (Array) Functions
Cicode has built-in functions for processing Cicode variable arrays:
-
To perform calculations (max, min, total, etc.) on array elements.
-
To look up the index number of an array element.
-
To shift the elements of an array left or right.
Note: Plant SCADA also provides a set of array functions that support associations with animation objects. See Array Functions.