Repository navigation
Varying Dimension Arrays
Daniel Long edited this page Sep 29, 2026
·
3 revisions
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))); // 5DIM(*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(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)); // 100DCL-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;- Installation
- Quick Start
- CLI Reference
- Language Basics
- Data Types
- Variables and Constants
- Data Structures
- Arrays
- Varying-Dimension Arrays
- Control Flow
- Procedures
- Procedure Overloading
- Built-In Functions
- Error Handling
- Operation Extenders
- Enumerations
- Program Status Data Structure
- Data Areas
- SND-MSG
- Embedded SQL
- Record-Level Access
- Display Files (WORKSTN)
- Program-Described Files (I-Specs and O-Specs)
- DATA-INTO and DATA-GEN
- XML-INTO
- Fixed-Format Source
- Database Connections
- Multi-Module Programs
- Environment Variables
- Testing
- Complete Example: Employee Report
- IBM i Conformance Results
- IBM i Execution Results