<< Click to Display Table of Contents >> Manual > Script Guide > Script Ver2 (SC2 syntax) > array |
About Arrays
SC2 syntax allows you to handle arrays. An array is a collection of values. For example, if you have 100 pieces of data, you can use an array to handle them all together in one variable. This is useful when you want to process a large amount of data at once.
Array variables
Declaring an array variable
Array variables are declared as follows:
Example: 100 arrays
var a[100]; // Declare an array of 100 elements. a[0] = 123; // Assign 123 as the first element. a[1] = 234; // Assign 234 as the second first element. … a[99] = 345; // Assign 345 as the 100th element. var b = a[0] + a[1] … + a[99]; // Sum all elements of array a and assign to b. var d = ::Math.Sum(a); // Sum all elements of array a and assign to d (Example using the Sum function)
Array element indexes start from 0. |
::Math.Sum is a method to calculate the sum of values. The Math object contains a collection of convenient functions for performing arithmetic operations. By using it in combination with an array, you can easily calculate the sum, maximum, minimum, average, etc.
|
Mixing Value Types
Arrays can contain mixed value types, they do not have to be uniform within the array.
a[0] = 123; a[1] = "hello"; a[2] = T;
Multidimensional array variables
The array variable "var a[100];" explained above is called a one-dimensional array to be precise. The SC2 syntax can also handle multidimensional arrays, and there is no limit to the number of dimensions of an array.
Declaring multidimensional array variables
A multidimensional array is declared as follows:
Example 1) Example of a two-dimensional array (10 x 100 items)
var a[10,100]; a[0, 0] = 1; // Assign 1 to [0, 0] a[0, 1] = 2; // Assign 2 to [0, 1] … a[9, 99] = 999; // Assign 999 to [9, 99] var b = ::Math.Ave(a); // Calculates the average of the elements in the second dimension (returns 10 1-dimensional arrays)
In the above script,var b = ::Math.Ave(a);" returns the average values of each column in one go as a one-dimensional array of 10 items. In other words,
b[0] ... average of 100 rows of data in a[0] b[1] ... the average of 100 rows of data in a[1] : : :
The return value will be something like this.
|
It may be easier to imagine a two-dimensional array as a matrix, like a database table. For example, "var a[10,100];" can be thought of as a table with 10 columns and 100 rows. When you retrieve values from a control that handles a database, the retrieved results are treated as a two-dimensional array. |
Example 2) Example of a 3-dimensional array (10 x 100 x 100 elements)
var a[10, 100, 100]; a[0, 0, 0] = 1; // assign 1 to [0, 0, 0] a[0, 0, 1] = 2; // assign 2 to [0, 0, 1] … a[9, 99, 99] = 99999; // assign 99999 to [9, 99, 99] var b = ::Math.Ave(a); // find the average of the third dimension elements of a (returns a 10x100 2D array) var d = ::Math.Ave(b); // Calculates the average of the second-dimension elements of b (returns 10 one-dimensional arrays) var e = ::Math.Ave(d); // Calculates the average of the first-dimension elements of d (returns 1 value)
Create an array with the keyword "c"
Using the keyword "c" we can easily create arrays.
The format of this keyword is:
c([value1], [value2], …) |
example)
var a = c(1,2,3,4); // Equivalent to "var a[4]". Each element is assigned "1,2,3,4". var a = c(1 + 2, 3 + 4, 4 + 5); // Can contain calculations var a = c(::GetVal("TAG1"), ::GetVal("TAG2")); // Can contain methods var a = c(T, 123, "Hello", #2005/1/1 0:0:0#); // Can contain values of different data types
You can also combine arrays to create multidimensional arrays.
example)
var a = c(c(1,2), c(3,4), c(4,5)); // Same as "var a[3,2]". a[0,0] is 1, a[0,1] is 2, a[2,1] is 5. var a = c(c(c(1,2), c(3,4)), c(c(5,6), c(7,8))) // Same as "var a[2, 2, 2]".
The keyword "c" stands for "combine." |
Array indexing
Declaring an array variable
To get an element of an array, specify the array index in [ ].
An index is a number ranging from 0 to (number of elements - 1).
a[Index] |
There are the following ways to specify the index:
1)Specifying a number i as the index will extract the i-th element.
2)Specifying c(i, j, k, l, m….) as the index will extract the i, j, k, l, m… elements. i,j,k,l,m… do not have to be specified in ascending order. Also, duplicate values can be specified. Example: c(3,4,4,4,2,1,0)
3)If you specify an index that is a Boolean array equal to the number of elements, only the elements that are T will be extracted.
4)If you specify an index i : j, it will take the consecutive elements from i to j.
5)If no index is specified, all elements are retrieved.
Considering the method of specifying each array index based on "var a = c(1,2,3,4,5,6,7,8,9,10)",
1)a[ 5 ] returns the sixth element from the beginning, which is 6.
2)a[ c(2, 5, 3) ] returns the 3rd, 6th, and 4th elements from the beginning, i.e. c(3,6,4).
3)a[ c(T, T, F, F, F, T, F, F, F, T) ] extracts only the elements that are T, i.e. returns c(1,2,6,10).
4)a[ 2 : 5 ] returns the 3rd through 6th consecutive elements from the beginning, i.e. c(3,4,5,6).
5)a[] returns all elements of the array, i.e. c(1,2,3,4,5,6,7,8,9,10) is returned as is.
Example of indexing a one-dimensional array
As an application of array indexing, you can extract only the elements from an array that satisfy a specified condition.
example)
var a = c(1,2,3,4,5,6,7,8,9,10) var b = a[a > 6];
When the above example is executed, b will be assigned elements greater than or equal to 6, that is, c(7,8,9,10).
because a > 6 returns c(F,F,F,F,F,F,F,T,T,T,T) and as a[c(F,F,F,F,F,F,T,T,T,T,T)] returns c(7,8,9,10).
example)
var a = c("Tank abnormal", "Tank normal", "Tank open", "Pump abnormal", "Pump normal", "Pump open") var b = a[::IsLike(a, "%abnormal%")];
When the above example is executed, the element containing the character "abnormality", that is, c("Tank abnormality", "Pump abnormality") will be substituted.
Because ::IsLike(a, "%Abnormal%") returns c(T,F,F,T,F,F) and as a result a[c(T,F,F,T,F,F)] returns c("Tank abnormal", "Pump abnormal").
Indexing into multidimensional arrays
To get the value of an array element, specify the index of each dimension of the array in brackets [ ].
a[Index1 , Index2, …] |
The method of specifying indexes is the same as for one-dimensional arrays.
Based on "var a = c(c(1,2), c(3,4), c(4,5))"
1)a[2, 0] returns the third element from the first dimension and the first element from the second dimension, which is 4.
2)a[2, ] returns the third element from the top of the first dimension, and since the second dimension is omitted, it returns all elements, that is, c(4,5).
3)a[ , 1] returns all elements in the first dimension because it is omitted, and the second element from the top in the second dimension, that is, c(2,4,5).
4)a[ c(T, F, T), ] returns the elements in the first dimension that are T, and since the second dimension is omitted, it returns all elements, that is, c(c(1,2), c(4,5)).
5)a[ 1 : 2, ] returns the second to third consecutive elements from the beginning of the first dimension, and since the second dimension is omitted, it returns all elements, that is, c(c(3,4), c(4,5)).
Applications of indexing into multidimensional arrays
As an application of indexing multidimensional arrays, it is possible to extract elements by comparison as shown below.
example)
var a = c(c("Japan", "England", "France" , "China", "Singapore"), c("Asia", "Europ", "Europ", "Asia", "Asia")); var b = a[0, a[1, ] == "Asia"];
When the above example is executed, only the country "Asia" will be extracted and c("Japan", "China", "Singapore") will be assigned to b. This is because a[1, ] == "Asia" returns c(T,F,F,T,T), and a[0, c(T,F,F,T,T)] returns c("Japan", "China", "Singapore").
Think of a two-dimensional array as a table
Here, we will consider the application of indexes by comparing a two-dimensional array to a database table.
1 |
Test1 |
T |
2005/1/1 12:30:00 |
2 |
Test2 |
F |
2005/1/1 14:25:00 |
3 |
Test3 |
T |
2005/1/1 15:15:00 |
For example, if a table with 3 rows and 4 columns like the one above is expressed using the keyword "c", it will look like this:
a = c( c(1,2,3), c(“Test1”,”Test2”,”Test3”), c(T,F,T), c(#2005/1/1 12:30:00#, #2005/1/1 14:25:00#, #2005/1/1 15:15:00#) );
In this example, the following indexes are possible:
1.a[0, ] is the 0th column, that is, c(1,2,3).
2.::Math.Ave(a[0, ]) is the average value of the 0th column, which is 2.
3.a[c(1,2), ] is the first and second columns, and the result is as follows.
c(c(“Test1”,”Test2”,”Test3”), c(T,F,T) );
4.a[c(1,1) , ] are all the first columns, and the results are as follows.
c(c(T,F,T), c(T,F,T) );
5.a[ , a[3, ] > #2005/1/1 14:00:00#] retrieves records from "2005/1/1 14:00:00" onwards.
c(c(2,3), c(”Test2”,”Test3”), c(F,T), c(”#2005/1/1 14:25:00#”,”#2005/1/1 15:15:00#”) );
6.::Math.Ave(a[0 , a[3, ] > #2005/1/1 14:00:00#]) calculates the average value of column 0 of the records after "2005/1/1 14:00:00". In other words, the average value of c(2,3), the result is 2.5.
If you want to retrieve table data from a database, please refer to ”Direct query database(SQL) control” or ”Remote query database(SQL) control”. |