Skip to content

Varying Dimension Arrays

Daniel Long edited this page Sep 29, 2026 · 3 revisions

Varying-Dimension Arrays

DIM(*VAR) — Fixed Capacity, Variable Size

Declare an array with a maximum capacity but let the active size grow at runtime:

DCL-S scores INT(10) DIM(*VAR: 100);   // up to 100 elements

// Set the active size
%ELEM(scores) = 5;
scores(1) = 90;
scores(2) = 85;
// ...

DSPLY ('Count: ' + %CHAR(%ELEM(scores)));   // 5

DIM(*AUTO) — Grows Automatically

DIM(*AUTO: max) behaves like DIM(*VAR: max) but the array expands as you assign to higher indices — no explicit %ELEM(arr) = n required:

DCL-S tags VARCHAR(20) DIM(*AUTO: 50);

tags(1) = 'alpha';
tags(2) = 'beta';
tags(3) = 'gamma';
// %ELEM(tags) is now 3 automatically
DSPLY %CHAR(%ELEM(tags));    // 3

%ELEM(*ALLOC) and %ELEM(*KEEP)

%ELEM(array : *ALLOC) is the number of elements allocated, as distinct from the number in use. A DIM(*VAR) array is allocated to its maximum, so on IBM i and here it reports the DIM maximum. Neither the allocation nor the count can be set above that maximum (IBM: RNF7563).

%ELEM(array : *KEEP) = n changes the element count like %ELEM(array) = n, leaving the allocation as it is:

DCL-S nums INT(10) DIM(*VAR: 100);

DSPLY %CHAR(%ELEM(nums : *ALLOC));   // 100 — allocated
DSPLY %CHAR(%ELEM(nums));            // 0   — in use

%ELEM(nums) = 5;
FOR i = 1 TO 5;
  nums(i) = i * 10;
ENDFOR;

%ELEM(nums : *KEEP) = 3;
DSPLY %CHAR(%ELEM(nums));            // 3
DSPLY %CHAR(%ELEM(nums : *ALLOC));   // 100

Arrays of Data Structures

DCL-DS employee QUALIFIED DIM(*VAR: 50);
  id   INT(10);
  name VARCHAR(40);
END-DS;

%ELEM(employee) = 2;
employee(1).id = 101;
employee(1).name = 'Alice';
employee(2).id = 102;
employee(2).name = 'Bob';

DSPLY employee(1).name;

Clone this wiki locally