Array (data type)

SimTalk provides array data types. An array is a one-dimensional or a two-dimensional value field of one of the base data types listed above under Data Types.

Base data types can be all data types except for table, list, stack, and queue. When you enter any as the base data type, each item of the value field can have a different data type. Then, even lists and tables can be placed in the array.

Array indexes are one-based, i.e., they start at 1, not at 0.

You can declare array variables as follows:

a : integer[10]
b : boolean[10,20]
a : string[]

When you enter a single number within the square brackets, this is a one-dimensional array of fixed size, for example a : integer[10].

When you enter two numbers within the square brackets, this is a two-dimensional array of fixed size, for example b : boolean[10,20].

When you do not enter any number at all within the square brackets, this is a one-dimensional array, whose size is not fixed, and which is empty initially, for example a : string[].

Compare this example:

var vector3  : real[3]               -- one-dimensional array with 3 real values 
var matrix3x3 : real[3,3]            -- two-dimensional array with 9 real values 
var objList : object[]               -- one-dimensional array with n objects (the size can change) 
var a        : any[]                 -- one dimensional array with n values of any data type 
print vector3[2]                     -- prints 0 on the console 
vector3 := [1.0, 2.0, 3.0]           -- fill a one-dimensional array 
print vector3[2]                     -- prints 2 on the console 

-- For filling a two-dimensional array you might enter:
for var x := 1 to matrix3x3.xDim
   for var y := 1 to matrix3x3.yDim
      matrix3x3[x,y] := x + y
next
next
print matrix3x3         -- prints [2, 3, 4][3, 4, 5][4, 5, 6]

print objList.dim       -- prints 0 on the console 
objList.append(Station) -- adds Station to the array 
print objList.dim       -- prints 1 on the console 
print objList[1]        -- prints the path to the Station on the console 
a := vector3
a.append("Hello World")
print a.dim             -- prints 4 on the console 
print a                 -- prints [1, 2, 3, Hello World] on the console 

Plant Simulation displays empty arrays with an empty space between the brackets like this "[ ]", compare the following example:

var a : real[]
var s : string := to_str(a)  -- s := "[ ]" 

The Functions for Accessing 3D Objects make extended use of the array data types.

Compare the sample models: Click the Window ribbon tab, click Start Page > Getting Started > Example Models and click Small Examples. Then, select the respective Category, the Topic, and the Example in the dialog Examples Collection and click Open Model.

Related Topics

Methods and Attributes of Arrays

Print the Contents List to the Console as an Array

isArray