If you don't want to wait for IBM to incorporate in-place array initialization (initialization values where the array is defined, as opposed to runtime or compile-time data), this article is for you.
The first method to accomplish array initialization (aside from the legacy methods in RPG) is easy if you're OK with pointers. It doesn't require knowledge of pointers, just the use of them.
First, declare a standalone field with a data type of asterisk (pointer). Then, initialize it to the address of the initial value for the array. Next, declare the variable that is to contain the initial value for the array and specify the initial value via the INZ keyword.
Finally, declare the array as a standalone field and add the BASED keyword. For the BASED keyword, identify the pointer variable that is already declared.
This technique is illustrated in the example that follows:
D ArrayInz S 70A Inz('Sunday ' :
D 'Monday ' :
D 'Tuesday ' :
D 'Wednesday ' :
D 'Thursday ' :
D 'Friday ' :
D 'Saturday ' )
D DayOfWeek S Dim(7) Based(pInz)
The second method is to declare a data structure with the array and the initial value as subfields. First, specify the array's initial values as a subfield using the INZ keyword. Then, add a second subfield as the array, and overlay the first field. See the example that follows:
D ArrayInz 70A Inz('Sunday ' :
D 'Monday ' :
D 'Tuesday ' :
D 'Wednesday ' :
D 'Thursday ' :
D 'Friday ' :
D 'Saturday ' )
D DayOfWeek Dim(7) Overlay(ArrayInz)
The downside of these methods is that they really only work with character fields/arrays.
An IBM solution, integrated with RPG IV, should allow numeric, logical, character, and date initial values to be specified. Until then, we can use these techniques for our character array initial values. They sure beat CTDATA (compile-time data)!
Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.
LATEST COMMENTS
MC Press Online