Array access is the act of reading or writing one specific element in an OCCURS group. Easytrieve does not use COBOL-style subscripts in the source line; instead you assign the INDEX field, then use the element name in statements. MOVE AMOUNT TO TAX-TBL element, IF STATUS EQ 'Y' on FLAG element, PRINT RATE on report line—all require INDEX set correctly first. Wrong INDEX produces silent wrong data: payroll uses December rate in January slot because IDX was left at 12 from prior iteration. This page explains reference mechanics, INDEX assignment patterns, using arrays in IF and PRINT, FILE buffer access for repeating groups, WORK qualifier disambiguation, nested access order, bounds discipline, and debugging techniques including DISPLAY of INDEX paired with element values for test jobs beginners run before promoting array logic to production.
DEFINE declares RATE W 3 P 2 OCCURS 12 INDEX R-IDX. R-IDX is an ordinary numeric field. When R-IDX holds 5, statements referencing RATE use the fifth three-byte packed element. Changing R-IDX to 6 switches active element without renaming fields. The INDEX field can appear in expressions: R-IDX = R-IDX + 1 increments subscript. Some shops store INDEX in a packed field for performance; N 2 0 is typical for small tables.
12345R-IDX = 4 IF RATE GT 0 PAY = PAY + RATE END-IF MOVE RATE TO WS-HOLD-RATE
MOVE supports element-to-element, literal-to-element, and element-to-scalar copies. IF compares element to literal or other fields when INDEX is set. Assignment statements using = follow same rules. Group MOVE into entire OCCURS structure may move all occurrences when source and target definitions match—verify Language Reference for your release before relying on group MOVE instead of element loops. Partial group overlays use parent field names from DEFINE hierarchy.
| Operation | Pattern | Note |
|---|---|---|
| Read element | Set INDEX, use field in IF or MOVE to scalar | INDEX must be valid |
| Write element | Set INDEX, MOVE literal or field to element | Overwrites that slot only |
| Print element | Set INDEX, PRINT on report LINE | One column per active element |
| Compare two slots | Set INDEX to A, MOVE to hold; set INDEX to B, compare | Two INDEX assignments |
After READ or GET, FILE LINE-ITEM OCCURS 8 INDEX L-SUB reflects input buffer content. Set L-SUB from 1 to LINE-COUNT from record header before processing valid lines. MOVE LINE-ITEM to working storage element copies one line. Do not process slots above COUNT—accessing unused occurrences reads residual buffer data from prior records if buffer was not cleared. WRITE path sets INDEX for each line being output then WRITE record.
12345678JOB INPUT INVOICE L-SUB = 1 DO WHILE L-SUB LE INV-LINE-COUNT IF LINE-ITEM NE SPACES PERFORM PROCESS-LINE END-IF L-SUB = L-SUB + 1 END-DO
When FILE and working storage share a name, qualify working storage with WORK: prefix in statements that need the W array. Unqualified names may resolve to FILE field per product name resolution rules. Arrays in working storage often use WS- or WK- prefixes to avoid clashes—shop standard beats fighting qualifier rules in every line.
For outer ROW OCCURS 5 INDEX R and inner COL OCCURS 10 INDEX C, set R first, then C, then reference inner element. Changing R without resetting C may leave column index from prior row—reset C to 1 when starting new row unless algorithm scans full matrix with single nested loop. Document loop order in comments for nested walks.
Valid INDEX range is 1 through OCCURS count for standard one-based usage. Loops DO WHILE IDX LE MAX should use MAX equal to OCCURS value. Using INDEX from input COUNT field without IF COUNT LE OCCURS risks overrun when file has bad data. Defensive code compares COUNT to maximum before loop. Test jobs deliberately pass COUNT greater than OCCURS to verify guard logic.
One-byte OCCURS over a buffer uses same INDEX walk. Set CX to position of delimiter found, MOVE substring using length override from start through CX. Access pattern CH at INDEX compares single character to comma or space. This is element access on smallest dimension—critical for parsing tutorials.
PRINT field on LINE prints current element when INDEX set in BEFORE-LINE or detail procedure. Printing entire array on one line requires multiple PRINT columns with INDEX changed between statements or multiple LINE entries in loop via PERFORM. TITLE and HEADING do not subscript—only data fields on LINE use INDEX context.
Access means pointing at one box in the row. You turn the INDEX dial to the box number, then read or write that box. If you forget to turn the dial, you might read box 7 when you meant box 2. Two rows of boxes need two dials—one for which shelf, one for which box on the shelf. Always look at the dial before opening a box.
1. Before using array element RATE in MOVE, you must:
2. Referencing an array element uses:
3. INDEX value 0 generally:
4. WORK: qualifier is used when:
5. FILE array access after READ: