| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (10 out of 14)

Using Other List and Array Expressions

You can reason about both lists and arrays and their elements by using the expressions described here. For a general discussion of G2 expressions, see Chapter 20, Expressions.

Accessing List or Array Elements by Index

To access a list or array element with an index:

These expressions produce the value in, or the item referenced in, the specified element in a list or array.

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

This expression produces a calculated value of either type 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:

Average of

This expression produces a calculated value of type 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:

This expression produces the number 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 truth-value expression. For a list, unless you are counting a subset of the list's elements, using the 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:


Note: You can optimize the execution of a 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:

This expression produces a truth-value that indicates whether an item of the specified class, or a value of the specified type, is a member of the specified list. Referring to an item that is a member of a list or array, is an indirect reference to that item.

An example is:


Obtaining the Number of List Elements

To find the number of elements in a list:

This expression produces the number of elements in a list. You can also use the 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:

This expression produces the number of elements in an array.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (10 out of 14)

Copyright © 1997 Gensym Corporation, Inc.