Q

How are arrays used in a RPG program?

I need to understand how arrays are used in a RPG program. I'm having difficulty with understanding how to incorporate

them in my programs. I have 20 salespeople with their sale records in the array, but I don't know how many records are in the array. How do I use an index in my calculations specs? Also how do I total it?


I assume you are using RPGLE. In order to use an array and find the size of the array, there is a very nice tip in our programmer tips section, Array definition: getting number of elements and sizes by Gus DiStasi. If you declare an array with an overlay, you can sort the array using the SORTA opcode. You can then total the array by looping through the array and breaking when the salesman ID changes. You can total the entire array with the XFOOT opcode.

*
* Character string to hold salesman, amounts
D CX1DS DS
D CX1 20 DIM(200) ASCEND
D CX1SLS 5 OVERLAY(CX1:1)
D CX1AMT 15S 0 OVERLAY(CX1:5)
* Sales Breakdown Array
* (Sorting not required as the CX1 sorta will put them in order D SLSDS DS
D SLS 20 DIM(200)
D SLSCOD 5 OVERLAY(SLS:1)
D SLSTOT 15S 0 OVERLAY(SLS:5)
*
* Data already loaded into CX1, CX1SLS, CX1AMT
C SORTA CX1
C CLEAR SLSDS
C CLEAR HLDSLS 5
C CLEAR Y 10 0
C Z-ADD 1 X 10 0
* SEED THE LOOP
C MOVE CX1SLS(X) HLDSLS
C CLEAR TOTAL 15 0
C X DOUGE 200
C HLDSLS IFNE CX1SLS(X)
C CX1SLS(X) IFEQ *BLANKS
C Z-ADD 200 X
C ELSE
C ADD 1 Y
C Z-ADD TOTAL SLSTOT(Y)
C MOVE HLDSLS SLSCOD(Y)
C MOVE CX1SLS(X) HLDSLS
C CLEAR TOTAL
C END
C END
C ADD CX1AMT(X) TOTAL
C ADD 1 X
C END
C MOVE *ON *INLR

You can combine this with the other code to use variable arrays in your calculations.


This was first published in April 2001

Dig deeper on RPG iSeries programming

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchEnterpriseLinux

SearchDataCenter

Close