Declaration of an ARRAY data type in ST
TYPE
(* array with one dimension, with intialization *)
name_1: ARRAY [x..y] OF data-type := [initial-value_1, initial-value_2, ..., initial-value_n];
name_2: ARRAY [x..y] OF data-type := [n(initial-value_1), n(initial-value_2), ...];
name_A: ARRAY [x..y] OF FB-type := [(input_1 := initial-value, input_2 := initial-value, ...), (input_1 := initial-value, input_2 := initial-value, ...), ...];
name_B: ARRAY [x..y] OF FB-type := [n((input_1 := initial-value, input_2 := initial-value, ...), n((input_1 := initial-value, input_2 := initial-value, ...), ...];
(* array with
2
dimensions, with intialization *)
name_3: ARRAY [x1..y1, x2..y2] OF data-type :=
[[initial-value_1_1, ..., initial-value_1_n], [initial-value_2_1, ..., initial-value_2_n]];
(* array with
2
dimensions, intialization is possible *)
name_C: ARRAY [x..y , x2..y2] OF FB-type;
(* array with
3
dimensions, intialization is possible *)
name_4: ARRAY [x1..y1, x2..y2, x3..y3] OF data-type;
name_D: ARRAY [x1..y1, x2..y2, x3..y3] OF FB-type;
(* array with
4
dimensions, intialization is possible *)
name_5: ARRAY [x1..y1, x2..y2, x3..y3, x4..y4] OF data-type;
name_E: ARRAY [x1..y1, x2..y2, x3..y3, x4..y4] OF FB-type;
(* as many repetitions as you like are possible *)
...
name_n: ARRAY [x..y] OF data-type := [initial-value_1, n(initial-value_2), ...];
END_TYPE
Meaning |
Declaration of one or more →array data types (name_1, name_2 etc.) with a collection of sub-elements of the same type (type or FB-type) ARRAY and OF are →keywords for the declaration of the array data type. This declaration is possible within the declaration of a user-defined data type (TYPE ... END_TYPE). Use the optional →initial value to assign a value to the array elements. := starts the initialization. A pair of square brackets [] is required for each array dimension. Specify the values for initialization within those []. Repetitions are possible by specifying parenthesis (). During the initialization of multi-dimensional arrays, the rightmost index subrange is filled at first from the list of initialization values (see the following examples for multi-dimensional arrays). If you enter more elements within the initialization than there are array elements, logi.CAD 3 highlights the initialization input as faulty. If you enter fewer elements within the initialization than there are array elements, the remaining array elements are initialized with the initial value of the appropriate data type. Current restrictions
|
See "Accessing the ARRAY data type and ARRAY elements", if you need information about accessing the array elements.
Moreover, logi.CAD 3
allows you to specify additional data for the declaration
. See "
Defining description, comment or JSON string for variables and data types
" for details.
TYPE
myType : ARRAY [1..9] OF INT := [1, 2, 3]; (* declaring 9 elements of data type 'INT' - the first 3 array elements are initialized with the entered values *)
Test : ARRAY [-10..-2] OF REAL := [2(1.0), 3(2.0), 3.0]; (* declaring 9 elements of data type 'REAL' - the first 6 array elements are initialized with the entered values *)
inputData : ARRAY [0..1] OF BOOL := [2(TRUE)]; (* declaring 2 elements of data type 'BOOL' - both array elements are initialized with 'TRUE' *)
scalings : ARRAY [1..5] OF INT := [2(5), 2(), 4]; (* declaring 5 elements of data type 'INT' - the first 2 array elements are initialized with '5', *)
(* the next 2 array elements with default initial value '0' for INT, the last array element with '4' *)
Test2 : ARRAY [3..5] OF Range := [2((min := 100, max:=400))]; (* declaring 3 elements of data type 'Range'; 'Range' is a user-defined data type; *)
(* the structure elements of the first 2 array elements are initialized with the entered values *)
TONArr1 : ARRAY [0..2] OF TON; (* declaring 3 elements of TON block - no initialization of the inputs *)
TONArr2 : ARRAY [0..2] OF TON := [(PT:=T#100ms), (PT:=T#50ms)]; (* declaring 3 elements of TON block - the input 'PT' is initialized for the first 2 elements *)
TONArr3 : ARRAY [1..50] OF TON := [50((PT:=T#100ms))]; (* declaring 50 elements of TON block - the input 'PT' is initialized for all elements *)
END_TYPE
TYPE
Dim2 : ARRAY [1..2, 1..3] OF INT := [2([3(25)])];
(* declaring 2 array dimensions "2 by 3" of data type 'INT' with these initial values:
Dim2[1,1] := 25, Dim2[1,2] := 25, Dim2[1,3] := 25,
Dim2[2,1] := 25, Dim2[2,2] := 25, Dim2[2,3] := 25 *)
Dim3 : ARRAY [1..2, 1..3, 1..4] OF INT := [2([3([4(15)])])];
(* declaring 3 array dimensions "2 by 3 by 4" of data type 'INT' with these initial values:
Dim3[1,1,1] := 15, Dim3[1,1,2] := 15, Dim3[1,1,3] := 15, Dim3[1,1,4] := 15,
Dim3[1,2,1] := 15, Dim3[1,2,2] := 15, Dim3[1,2,3] := 15, Dim3[1,2,4] := 15,
Dim3[1,3,1] := 15, Dim3[1,3,2] := 15, Dim3[1,3,3] := 15, Dim3[1,3,4] := 15,
Dim3[2,1,1] := 15, Dim3[2,1,2] := 15, Dim3[2,1,3] := 15, Dim3[2,1,4] := 15,
Dim3[2,2,1] := 15, Dim3[2,2,2] := 15, Dim3[2,2,3] := 15, Dim3[2,2,4] := 15,
Dim3[2,3,1] := 15, Dim3[2,3,2] := 15, Dim3[2,3,3] := 15, Dim3[2,3,4] := 15 *)
Dim2a : ARRAY [1..2, 1..3] OF INT := [[9, 8, 7], [6, 5, 4]];
(* declaring 2 array dimensions "2 by 3" of data type 'INT' with these initial values:
Dim2a[1,1] := 9, Dim2a[1,2] := 8, Dim2a[1,3] := 7,
Dim2a[2,1] := 6, Dim2a[2,2] := 5, Dim2a[2,3] := 4 *)
Dim2b : ARRAY [1..5, 1..4] OF DINT := [2([9, 8, 7, 6]), [2(), 4], [5, 3(1)]];
(* declaring 2 array dimensions "5 by 4" of data type 'DINT' with these initial values:
Dim2b[1,1] := 9, Dim2b[1,2] := 8, Dim2b[1,3] := 7, Dim2b[1,4] := 6,
Dim2b[2,1] := 9, Dim2b[2,2] := 8, Dim2b[2,3] := 7, Dim2b[2,4] := 6,
Dim2b[3,1] := 0, Dim2b[3,2] := 0, Dim2b[3,3] := 4, Dim2b[3,4] := 0, - Note: The initial values '0' derive from data type 'DINT'.
Dim2b[4,1] := 5, Dim2b[4,2] := 1, Dim2b[4,3] := 1, Dim2b[4,4] := 1,
Dim2b[5,1] := 0, Dim2b[5,2] := 0, Dim2b[5,3] := 0, Dim2b[5,4] := 0 - Note: The initial values '0' derive from data type 'DINT'. *)
Dim3a : ARRAY [1..2, 1..3, 1..6] OF SINT := [[[9, 8, 7, 6, 5, 4]], [2([3, 2, 1])]];
(* declaring 3 array dimensions "2 by 3 by 6" of data type 'SINT' with these initial values:
Dim3a[1,1,1] := 9, Dim3a[1,1,2] := 8, Dim3a[1,1,3] := 7, Dim3a[1,1,4] := 6, Dim3a[1,1,5] := 5, Dim3a[1,1,6] := 4,
Dim3a[1,2,1] := 0, Dim3a[1,2,2] := 0, Dim3a[1,2,3] := 0, Dim3a[1,2,4] := 0, Dim3a[1,2,5] := 0, Dim3a[1,2,6] := 0, - Note: All initial values '0' derive from data type 'SINT'.
Dim3a[1,3,1] := 0, Dim3a[1,3,2] := 0, Dim3a[1,3,3] := 0, Dim3a[1,3,4] := 0, Dim3a[1,3,5] := 0, Dim3a[1,3,6] := 0,
Dim3a[2,1,1] := 3, Dim3a[2,1,2] := 2, Dim3a[2,1,3] := 1, Dim3a[2,1,4] := 0, Dim3a[2,1,5] := 0, Dim3a[2,1,6] := 0,
Dim3a[2,2,1] := 3, Dim3a[2,2,2] := 2, Dim3a[2,2,3] := 1, Dim3a[2,2,4] := 0, Dim3a[2,2,5] := 0, Dim3a[2,2,6] := 0,
Dim3a[2,3,1] := 0, Dim3a[2,3,2] := 0, Dim3a[2,3,3] := 0, Dim3a[2,3,4] := 0, Dim3a[2,3,5] := 0, Dim3a[2,3,6] := 0 *)
END_TYPE
Using named value as array limits
If you want to use named values for the limits, a data type with named values is required. In order to be able to use a named value, just enter the name as the limit. In order to address the name uniquely, this syntax is provided : data_type_name#named_value – see the following example
Deviation from IEC-standard
The →IEC-standard does not permit the usage of named values.
Best practice is to use a data type that has been declared with the base data type DINT. This guarantees that a valid index subrange is specified for the array data type or an array variable. However, if required, you are able to use a data type with named values that has been declared with a base data type belonging to the →generic data type ANY_INT. logi.CAD 3 will inform you, if the used values for the index subrange are not within the range of DINT.
TYPE
myType1 : ARRAY [Colors3#Red..Colors3Green] OF INT; (* The array data type uses named values. *)
myType2 : ARRAY [Colors4#Red..Colors4#Orange] OF INT; (* The array data type uses named values. *)
Colors3 : DINT(Red := 1, Green := 100); (* Here the declaration of the data type with the named values. *)
Colors4 : DINT(Red := 1, Orange := 100); (* Here the declaration of the data type with the named values. *)
END_TYPE