Dynamic Arrays vs. Dimensioned Arrays

DataBasic lets you process sets of values in the form of dynamic arrays or as individual array elements in dimensioned arrays. This topic discusses the tradeoffs involved in each format.

Dynamic Arrays

Dynamic arrays are primarily useful in interfacing to Reality file items. By specifying a single variable name, you can read or write an entire item, or access individual parts of an item easily.

Referencing a dynamic array and scanning it looking for individual elements may sometimes be inefficient, depending on the application.

For example, to extract attribute 10 from an item, DataBasic first scans over attributes 1 through 9. When replacing data in an item, DataBasic passes by all the data preceding the replacement, copies in the new value and then appends all the data that came after the attribute. This continual scanning becomes inefficient when you are accessing a large number of elements or processing a large item.

Dimensioned Arrays

In such cases, it is better to read the item into a dimensioned array (using the MATREAD statement). MATREAD places each attribute of the item into separate variable locations that can be accessed individually, without scanning the item. This is particularly useful if several attributes need to be changed, because you can modify attributes without moving the rest of the item.

You can still use dynamic array references to extract, delete, insert and replace attributes, values and subvalues. For example, if the item is stored as a dynamic array, the expression ITEM<3,2,4> accesses the fourth subvalue in the second value in the third attribute. On the other hand, if the item is stored as a dimensioned array, the expression ITEM(3)<1,2,4> accesses the same element.

This method has its disadvantages too. Because there is a separate descriptor for each element in the array, it uses more variable descriptor space. It also uses more free space, because each attribute may have its own buffer in free space. In addition, items must be disassembled for a MATREAD and reassembled with a MATWRITE, which takes more time than a simple READ or WRITE.

Rules of Usage

Use the following rules as guidelines in determining whether to use dynamic or dimensioned arrays.

  1. Use dynamic arrays only when dealing with data read from or written to file items. Use standard dimensioned arrays when the need for an internal table arises.
  2. If an element in a dynamic array is to be used more than once, assign it to a variable instead of performing multiple extractions. Also, if the application is to reference many values/subvalues in an attribute, assign the attribute to a variable and perform the extractions and replacements on this smaller string (using an attribute value of one) rather than continually scanning the entire item.
  3. Use dynamic arrays to extract several attributes from the beginning of an item or to replace four or five values in a large item. However, use a dimensioned array to construct new items or to access several different attributes.
  4. Use dimensioned arrays only where necessary, because the object code required to reference them is greater than for single variables. However, avoid doubling the source code simply to avoid using subscripted variables.