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.



Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:
LATEST COMMENTS
MC Press Online