MultiValue records and arrays

Build a record with multiple values in its fields, search those values, and work with dimensioned arrays and select lists.

Build a record with named fields

A dynamic array is a string whose fields are separated by field marks (@FM). Angle brackets address fields starting at 1. EQUATE gives the field positions meaningful names.

    EQUATE name_field TO 1
    EQUATE city_field TO 2
    contact = 'Mira Patel' : @FM : 'Leeds'
    contact<city_field> = 'York'
    CRT contact<name_field> : ' lives in ' : contact<city_field>

Expected output is Mira Patel lives in York.

Store several values in one field

A field can contain values separated by @VM. A value can contain subvalues separated by @SM. The coordinates in contact<2, 2, 1> select field 2, value 2, subvalue 1.

    contact = 'Mira Patel'
    contact<2, 1> = 'home' : @SM : '0113 555 0101'
    contact<2, 2> = 'work' : @SM : '01904 555 0102'
    CRT contact<2, 2, 1> : ': ' : contact<2, 2, 2>

Expected output is work: 01904 555 0102. See Field, value and subvalue marks.

Find or add a value

LOCATE returns the position when it finds a match. In prime emulation, used by T24 and jbcc by default, product<2, 1> starts the search at value 1 of field 2. An index of -1 in an assignment appends at that level. Change the tag to blue to try the found branch.

    product = 'Notebook' : @FM : 'blue' : @VM : 'ruled'
    tag = 'linen'
    LOCATE tag IN product<2, 1> SETTING position THEN
      CRT 'Already at position ' : position
    END ELSE
      product<2, -1> = tag
      CRT 'Added ' : product<2, 3>
    END

Expected output is Added linen.

Use a fixed-size array

Dimensioned arrays use parentheses. DIM declares the size and MAT fills every element with an initial value. This example records two sales in one of three counters.

    DIM sales(3)
    MAT sales = 0
    sales(2) = 4
    sales(2) += 3
    CRT sales(2)

Expected output is 7.

Walk a select list

SELECT turns the fields of a dynamic array into a select list. With an open file handle as its input, it selects the file's record keys. READNEXT returns true while it can fetch another key, which makes it a convenient loop condition.

    keys = 'C001' : @FM : 'C002'
    SELECT keys TO pending_keys
    LOOP WHILE READNEXT key FROM pending_keys DO
      CRT 'Next key: ' : key
    REPEAT

Expected output is Next key: C001 followed by Next key: C002 on the next line.