Over the past several years, some of you have requested multidimensional array support in RPG IV. Your request has not fallen on deaf ears, but the requirement has not gathered the head of steam it needs to become reality. After all, there are some substitutes out there right now for obtaining a decent simulation in most cases.
Essentially, there are three kinds of arrays: a list, a table, and a matrix. Here, I will explore some nifty examples for each of these cases.
Lists
In order to get a list (a one-dimensional array), you can declare an array with a basing pointer in the D-specs:
D pList s * inz D BigList s like(ListElem) Dim(MaxSize) Based(pList) D ListElem s 15P5 D MaxSize c Const(32000)
Figure 1: Declaration of list BigList
As shown in Figure 1, you simply define a variable pList as a pointer and then define BigList to be an array with a maximum of 32,000 elements based on the pointer pList. Each element of the BigList array has the size characteristics of the variable ListElem. I separated these items in this way to allow for some flexibility. If several such arrays are needed and the length needs to be changed, only ListElem's definition need be affected. If the maximum number of list elements needs to increase or decrease, only MaxSize needs to be changed.
Programmatically, the actually size of the array can be achieved by using the %REALLOC built-in function (BIF). Remember that you resize the array by multiplying the current number of elements (or some maximum) by the size of each element in the array.
Tables
To get a table (a two-dimensional array), you can define an RPG IV table with an alternate table:
D BigTable s like(TblElem) Dim(MaxSize) D CTDATA PerRcd(1) D AltTable s like(TblElem) Dim(MaxSize) Alt(BigTable) D ListElem s 15P5 D MaxSize c Const(32000)
Figure 2: Declaration of table BigTable
What I did in Figure 2 truly is not new to most RPG programmers. It's a quick fix to a problem, but it's not the best fix in my opinion. In fact, for those of you who are familiar with the true needs for multidimensional arrays, this solution probably is worthless. However, this table generally fulfills most needs for businesses that need tables for figures and not for things such as vector products and the like. My recommended solution would be to use a basing pointer for each dimension of your array as I'll show in the discussion of matrices below.
True Multidimensional Arrays : Matrices
What do you need to do to get a matrix, a true multidimensional array? Coding this in RPG IV is not as easy as coding it in C++, but it is possible...with pointers.
In C++, a three-dimensional array would be coded as shown in Figure 3:
int *MyMatrix = new int [4] [2] [6];
Figure 3: Coding a matrix in C++
In RPG IV, this might be coded as shown in Figure 4:
D x c Const(4) D y c Const(2) D z c Const(6) D MaxSize c Const(32000) D MatrixElem s 15P5 D Matrix s * inz
D Graph s like(MatrixElem) Dim(MaxSize) D Based(Matrix)
Figure 4: Defining a matrix in RPG IV
As you can see, there's a bit more work, but the result is very similar and certainly is not as much work as trying to figure out what to do with tables and an array or some other convoluted construction. Moreover, you can write many procedures around the construct from Figure 4 so that manipulation of the multiple dimensions can be more seamless and much less confusing! For example, the initial allocation of space for this matrix would be with the %ALLOC BIF as shown in Figure 5:
/free Graph = %alloc(numelements * %size(MatrixElem) * x * y * z) /end-free
Figure 5: Using the %ALLOC BIF to allocate space
Here, x, y, and z are the dimensions of the matrix. So the element corresponding to Graph(1,2,2) would be found at %size(MatrixElem) * 1 * 2 * 2. Simple enough, eh?
Figure 6 show one way in which data might be assigned to the matrix Graph. You can also see how space for the matrix can be reassigned "on the fly" if the maximum index is greater than the number of elements already occupied by Graph. One additional check in this program might be to ensure that the index value is not greater than the maximum value assigned to the array in the D-specs.
/free // Create a matrix whose values are the product of the indices for x = 100 downto 1 for y = 75 downto 1 for z = 200 downto 1 index = x * y * z; // compute the index value if %elem(Graph) < index // Enough space alloc? Graph = %realloc(index * %size(MatrixElem)); Graph(index) = index; Z = z + 1; Endfor; Y = y + 1; Endfor; X = x + 1; Endfor;
/end-free
Figure 6: Assigning values to the elements of the matrix Graph
As always, I recommend keeping other programmers in mind when you code. The less confusing it is for them, the less annoying it will be for you! I think it's a good thing that IBM has not included the multidimensional ability into RPG IV. Does anyone besides me envision the sharing of a service program package for multidimensional arrays?
--Vincent B. Goldsby This email address is being protected from spambots. You need JavaScript enabled to view it.
This book provides an amazingly comprehensive introduction to the concepts while at the same time delivering enough technical detail to make you productive very quickly.
Today, it's all about input and output. Getting data into the IBM i from non-traditional sources and then displaying it back out again in varied formats. But where can you go to learn all that you need to know about this critical skill?
Too valuable to be classified as merely excellent certification material, this book should also rightly take its place on DB2 DBA bookshelves as a solid day-to-day DB2 reference.
Whether you want to obtain an IBM certified DB2 professional certification or simply become well-rounded in the fundamental concepts of DB2 and general database theory, this is your book.
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
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