first and second) or the end (next-to-last, last) of the list, or adjacent to another element (before, after). You can also access list elements by using an element index into the list. However, unlike an array index operation, which evaluates to a position and thus accesses the array element directly, a list element index causes G2 to perform a linear search from the beginning of the list to the specified item-or-value referenced by the index.
Array-length attribute. You can change the array length interactively by editing the attribute, or programmatically by using the change action. Changing the length of an array interactively is a permanent change. Changing the length programmatically is temporary: resetting the knowledge base causes the array length to revert to its original value.
When you shorten the length of an array while it is active, the high end contents are lost. When you lengthen an array while it is active, G2 provides initial values to the new elements.
Arrays are more efficient than lists for referencing an indexed element. In contrast to lists, arrays do not require a linear search for their elements. G2 uses an array index to locate an array element directly. Use arrays when you need quick access to a set number of elements.
The next diagram illustrates an item array,
CAR-ROWS, whose elements consist of two user-defined CAR-ARRAY item arrays, car-array-1 and car-array-2. The elements of these two arrays, in turn, consist of CAR items. The example shows several CAR items on the right of the workspace, which are elements of car-array-2. For example, to reference the third car in that array, use the expression:
car-rows [ 1 ] [ 2 ]
[ 1 ]) refers to the car array (car-array-2), and the second index ([ 2 ]) refers to the item within that array (the third car item, AUDI).
![]() |
List or Array Contents
When a list or array is populated with item or value elements, the item elements consist of item references. The items are not a part of the list or array. Deleting an item from a list or array does not delete the item, but removes the item reference. Because item elements are references, a single item can be a member of several lists or arrays, and can appear more than once in the same list. In contrast, value list and array elements are not references, and exist as part of the list or array.
Alternatively, you can save the elements of transient-membership lists and arrays by using the g2-snapshot system procedure, and then to load the snapshot KB, using the warmboot option. A snapshot of a KB saves all existing data, transient and permanent, which you can restore later during a warmboot load. For information about saving and loading a KB this way, see Saving a KB Snapshot File and Warmbooting a KB Snapshot File.
Effect of Run States on Lists and Arrays
As with most G2 items, you can reference lists and arrays only when they are active. This means that expressions and actions involving lists and arrays execute only when the list or array is active. In addition, items must be active before you can add them to a list or array.
Activation and deactivation affects lists and arrays in this way:
When workspace items are list or array elements, their behavior differs from other elements. In G2, deactivated workspaces are the only deactivated items that you can refer to within expressions. Similarly, deactivated workspaces can exist as list or array elements.
Summarizing List and Array Differences
The following table summarizes the differences between lists and arrays: