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

Comparing Lists and Arrays

You create lists and arrays as placeholders for items and values in your KB. The two items have similar behavior, but are best suited for different purposes. The choice of whether to use a list or an array can be made easily once the differences between both items are clear. This section offers a brief description of lists and arrays, and concludes with a table of their differences.

Choosing Lists

Lists are objects that contain a group of one or more elements. Lists have no fixed length. A list grows or shrinks in length as you insert or remove elements, and the numeric position (or index) of any element changes as G2 inserts or removes elements closer to the beginning of the list. Use lists to implement:

G2 provides expressions to reference certain list elements, using the position near the beginning (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.

Choosing Arrays

Arrays are objects that contain a sequence of zero or more elements. Arrays have a fixed length, which you declare in the item's 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.

Using Nested Arrays

While arrays in G2 are one-dimensional, you can nest them so that they function as multi-dimensional arrays. For example, you can create an array of arrays and reference its elements using a double index. The array of arrays then functions as a two-dimensional array.

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:

where the first index ([ 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.

By default, neither item references nor value elements are a permanent part of a list or array: lists are cleared upon reactivation, and arrays are re-initialized. Such a list or array is called a transient-membership list or array.

You can optionally set a list or array to retain references and elements as permanent knowledge. Such a list or array is called a permanent-membership list or array. For details, see Using Permanent-Membership Lists and Arrays.

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.

For a description of G2 run states, see Operating the Current KB.

With transient membership: With permanent membership:
Inactive lists are empty. Inactive arrays contain their initial values.
Inactive lists retain their elements after they have been populated. Inactive arrays contain their initial values, if they have not been previously populated, or their elements if they have.

Activation and deactivation affects lists and arrays in this way:

This action: In a list or array with transient membership: In a list or array with permanent membership:
Deactivating list or array
Removes all elements.
Retains all elements.
Deactivating an item which is an element
In a list, removes item. In an array, renders the item inaccessible until active.
Retains the item.
Deleting an item
In a list, removes item. In an array, removes item so that element has no value.
In a list, removes item. In an array, removes item so that element has no value.
Activating list or array
Removes all elements. Populates array with initial values.
Populates list with previous elements. Populates array with previous elements, or with initial values if not previously populated.

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:

Characteristic Arrays Lists
Increases or decreases size dynamically


Has a set length


Allocates memory at creation time


Contains mixed data types


Contains elements of values and items


Can have initial values


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

Copyright © 1997 Gensym Corporation, Inc.