Tips and Techniques: Inline Array Initialization

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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 pInz            S               *   Inz(%addr(ArrayInz))  
     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                 DS
     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.

BOB COZZI

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.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA 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.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra 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: