Defining and Assigning Array
Variables in a Script
The variables in your script can be defined as arrays (vectors or matrices). When defining a local script variable that is an array A collection of variables that share common output attributes and can be manipulated in GoldSim elements or input expressions. in a Variable Definition statement, or assigning a value to a script variable that is an array in a Variable Assignment statement, the Definition portion of the dialog expands to provide some additional options for defining the array:
If you are defining a vector A one-dimensional array., there are two choices in this drop-list:
- If you select the first
one ("Entire array"), then the Definition field would require the field
to be a vector. For example, when defining a variable (named X) that was a
vector of Days you could enter the following to set the entire vector to
zero:
In the script, it would then look like this: - Alternatively, if you
select "Item at index", then you must also specify the item being specified, and
the Definition field would require the field to be a scalar An output consisting of a single value or condition.. For example, you could enter the
following:
In the script, it would then look like this:
In this case, the third item of the vector would be assigned the value 7.5.
Similarly, if you are defining a matrix A two-dimensional array., there are four choices in this drop-list:
- If you select the first
one ("Entire array"), then the Definition field would require the field
to be a matrix. For example, when defining a variable (named Y) that was a
matrix of Regions and Lakes you could enter the following to set
the entire matrix to 0:
In the script, it would then look like this: - Alternatively, if you
select "Item at index", then you must also specify the item (row and column)
being specified, and the Definition field would require the field to be a
scalar. For example, you could enter the following:
In the script, it would then look like this:
In this case, the item in the 3rd row and 5th column of the matrix would be assigned the value 8.2. - If you select "Row at
index", then you must also specify the row being specified, and the
Definition field would require the field to be a vector. For
example, you could enter the following:
In the script, it would then look like this:
In this case, the item in the 4th row would be assigned as a vector of 1's. - If you select "Column at
index", then you must also specify the column being specified, and the
Definition field would require the field to be a vector. For
example, you could enter the following:
In the script, it would then look like this:
In this case, the item in the 3rd column would be assigned as a vector of 1's.
Note: When you are specifying the "Item at index" (for a vector or matrix), or "Row at index" or "Column at index" (for a matrix), you do not have to enter a number. You can enter any scalar expression. GoldSim rounds the result to the nearest integer value. This allows you to define arrays by embedding Variable Assignment statements in loops (in which case these indices are often the loop counter variable).
The discussion above applies to assigning values to arrays in both Variable Definition statements and Variable Assignment statements. That is, both types of statements are used to assign values. It is important to understand, however, that the behavior of the two types of statements is different when assigning values to arrays.
To understand this, consider the following excerpt from a script, containing a Variable Definition statement, as well as a Variable Assignment statement (that references a previously defined variable):
Both X and Y are vectors. Line 3 assigns the value 4 to the second item of vector X. Line 5 assigns the value 2 to the second item of vector Y. The question is: since these statements only partially assign values to the vector (i.e., they only assign the second item), how do the statements affect the other items in the vector (that are not explicitly referenced)? The difference is as follows:
- For a Variable Definition statement, if it does not assign all the items of the array, the remaining unassigned items are automatically assigned a value of 0 (for value data types) or false (for condition data types).
- For a Variable Assignment statement, a partial assignment of a vector or matrix has no effect on the items of the vector or matrix that were not targets of assignment. That is, since all items of the array must have been previously assigned when the variable was defined (and hence already have a value), any items of the array that are not explicitly assigned are unaffected.