Accessing List or Array Elements by Index
To access a list or array element with an index:
{g2-array | g2-list} [integer] -> {item | integer | float | symbol | text | truth-value}
An example is:
![]() |
When an expression refers to an element in a list, the length of time G2 requires to evaluate the expression is proportional to the position of the specified element in the list.
Performing Computations Over Sets of Elements
You can compute the values of expressions over sets of elements in a list or an array. Sum, Product, Minimum, Maximum of
the {sum | product | minimum | maximum} over each
generic-reference-expression in {g2-array | g2-list} of (quantity-expression)
-> {integer | float}
integer or float from the set of items or values specified in the generic reference expression, which are contained in the specified array or list and which meet the criterion specified in the quantity expression. Because this expression can produce either an integer or float value, use a piece of knowledge declared as type
quantity to contain the produced value. For example, the following expression computes the sum of the flows of all valves that are connected to
tank-1 and are also elements in my-valve-list:
the sum over each valve V connected to tank-1 in my-valve-list of
(the flow of V)
the average over each generic-reference-expression in {g2-array | g2-list}
of (quantity-expression)
-> float
float from the set of items or values specified in the generic reference expression, which are contained in the specified array or list and which meet the criterion specified in the quantity expression. An example is:
![]() |
Count of
You can also use the count of each expression to specify the elements that G2 iterates over in a set. The syntax is:
the count of each generic-reference-expression in {g2-array | g2-list}
[such that (truth-value-expression) ]
-> integer
the number of elements in expression is faster. For example, the following expression finds the number of elements in
qlist-1 that have the value 4:
the count of each water-tank T in water-tank-list such that T < 200
the count of each expression if it references indexed attributes. See Defining an Indexed Attribute for more information about indexed attributes.
Testing for List Membership
To test whether an element is or is not a member of a list:
{class-name | type} is [not] a member of g2-list
-> truth-value
An example is:
![]() |
Obtaining the Number of List Elements
To find the number of elements in a list:
the number of elements in g2-list
-> integer
the count of each expression to count the elements in a list; however, using the the number of elements in expression operates more quickly. An example is:
![]() |
Finding the Length of an Array
To find the length of an array:
the array-length of g2-array
-> integer