## 2 Dimensional Array in RPGLE

5 pts.
Tags:
AS400 RPGLE
RPGLE array
How do I define and use a 2 dimensional array in RPGLE?
I have to add up to 10 contracts from one file and order sequence numbers for every item on an order that belongs to a one of those contracts and then write each of those contract/sequences to it's own order. I am lost on the best way to achieve this!  Where to start, how to load, how get it into a new order.
Contracts are 5,0 and can have up to 500 sequence numbers (5,0)
Thanks!
Biscut

Software/Hardware used:
iseries

Thanks. We'll let you know when a new response is added.

You can mimic a 2-D array in RPG by defining a multiple-occurrence data structure and then defining an array within that.
Seems overcomplicated to me.
I’d attack it by creating a (work) data file to store everything and then clear it out or delete it after the process is complete. This would be slower than a solution that keeps everything in memory, but MUCH easier to understand and maintain.

## Discuss This Question: 3 Replies

Thanks. We'll let you know when a new response is added.
• If arrays in RPG are difficult, code the routine in COBOL or maybe C. In COBOL, multi-dimensional arrays are made by nesting group-items at different levels and specifying an OCCURS clause. In RPG, you use a DS instead of a group-item and specify a DIM() keyword. For example:
```d ara1            ds                  dim( 99 ) qualified
d   ara2                        10a   dim( 99 )
d   ara3                        10i 0 dim( 99 )

ara1(x).ara2(y) = 'New value' ;
ara1(x).ara3(y) += 1 ;
```
The example shows ara2 and ara3 both with 99 elements within ara1 that has 99 occurrences. By using LIKEDS() on the subfields, much more detail can be added at the lowest level. A third level could be added to give 3-D arrays for example. As you describe it, you might have code like this:
```d ConSeq          ds                  dim( 10 ) qualified
d   Contract                     5p 0
d   SeqNbr                       5p 0 dim( 500 )
```
In this case, you have a ConSeq structure that has 10 occurrences. Each occurrence will have a Contract along with up to 500 SeqNbrs. You might want to have an additional subfield for a counter to keep track of how many of the SeqNbrs are actuallu used for this contract/sequence number group. Actually, there are multiple possibilities. For example, instead of a DIM() on the DS, you might use an OCCURS(10) keyword. And there are other ways to combine items with DIM(), too. It's hard to know what might be most useful without knowing what is going to e processed and how it's all going to be used. Tom