Data Structures in ILE RPG: Model the Shape of Your Data
A customer code, name, city, and credit limit declared as four separate variables carry no sign that they belong together. Pass them to a procedure and you pass four parameters in an order the caller must get right. A data structure states the relationship directly.
QUALIFIED
With QUALIFIED, every subfield is reached through its structure's name: custRec.code, custRec.limit. Subfields stay scoped to the structure, so an unrelated vendorRec.code in the same program never conflicts. That is why qualified structures are the modern default.
LIKEDS and TEMPLATE
LIKEDS declares a new structure with the exact layout of an existing one β for a before-image, an output record, or a parameter. When the original changes, every copy follows at the next compile. TEMPLATE marks a structure that exists only to define a shape and allocates no storage of its own.
Nesting
A subfield can itself be a structure. An order's ship-to address is a natural example, reached as orderRec.shipTo.city. Reserve nesting for genuinely hierarchical relationships; each level is a further demand on the reader.
Arrays of data structures
DIM on a qualified structure creates an array of complete records. Each element carries all of its related fields, so there is no second array to fall out of step β the structural weakness of parallel arrays simply disappears.
dcl-ds lineRec qualified dim(20);
itemCode char(10);
qty packed(5:0);
price packed(9:2);
end-ds;
lineRec(1).itemCode = 'WIDGET-1';Lesson 21 closes with an order structure that combines a header, a nested address, and an array of order lines β one name, one shape, one parameter.
Get the lesson
Lesson 21, βData Structures: Qualified, LIKEDS, and Nested,β is part of Beginner ILE RPG in IBM i ILE RPG Mastery and is available at ILERPG.com for USD 10. The complete Beginner ILE RPG series β all 18 lessons β is USD 180, or USD 135 with coupon code RPG30, which takes 25% off when you purchase the full series.