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.
LATEST COMMENTS
MC Press Online