|
|||||
|
|||||
Arrays: multi-dimensional |
Key-based indexing can be used to simulate multi-dimensional arrays.
One example of a two-dimensional array is a table. Each item in the table has a row and a column. Shelf item keys can contain the row number and the column number separated by a separator character like period (.). Alternatively, if each row and column has a "name", then those names can be used in the keys instead of the row and column numbers. The following code is an example of a two-dimensional array, with the shelf item keys composed of the row number and column number for each item, separated by a period:
down-translate global integer row global integer col global stream table variable element table clear table set row to 0 suppress element row increment row set col to 0 suppress element col increment col set table { "%d(row).%d(col)"} TO "%c"
An example of a three-dimensional array with which most readers will be familiar can be found with Rubik's Cube. Suppose we are using OmniMark to solve the cube. The cube is made up of 26 sub-cubes (the center, where you would expect to find the 27th, is empty and thus doesn't need to take up an item on our shelf). Each sub-cube is given a three-part key. The first part indicates where it is relative to the table: 1 if it's in the top layer, 2 if it's in the middle layer, and 3 if it's in the bottom layer. The second part of the key indicates if the sub-cube is in the left slice of the cube (1), the middle slice (2), or the right slice (3). Finally, the third part is used to tell if the sub-cube is facing us (1), in the middle (2), or facing away from us (3).
So, the top-left-front sub-cube would be indicated with a key of "1.1.1" and the bottom-right-rear sub-cube by "3.3.3". Note that there would be no item with a key of "2.2.2" because that is the empty position. An arbitrary position given by three integers, x, y, and z, can be specified by the key %d(x).%d(y).%d(z)
.
How this shelf would actually be used to solve Rubik's Cube is left as an exercise to the interested reader.
Another way to create multi-dimensional data structures it to use records. A record field is a shelf, and you can create a shelf of a record type, effectivly giving you shelves that can contain shelves. You can also use records to build other kinds of data structures such as trees.
Copyright © Stilo International plc, 1988-2008.