RPG IV (RPG ILE) and AS400 Data Structure Programming
Purpose of this article is to learn Data Structures and their common usage. We will also learn Program Status and File Information data structures. First thing first, let us learn
What is a data structure?
Data structure is in a sense a collection of several data types. Most of the times a data structure in RPG IV, in whole, is of character type.
Data structures give us an easy means to treat different portions of an RPG variable as different data types' variables.
Data structures in RPG IV are most commonly used for
- Date conversions:- Though not recommended, because they seem to be very complex and clumsy, they are found to be effective performers as compared to built in functions
- Processing flat files:- When dealing with several other systems, data transfer from and to an AS400 machine is mostly in flat file format. It seems XML is still to be popular here! And to process a flat file, it seems, there is no other alternative of data structures.
Basic idea behind using data structures in RPG is to treat and use different portions of a single variable as different variables.
Defining a data structure on AS400 system
Data structures are defined in D-Specs in RPG IV. In RPG, Data structures were defined in I spec though. Since RPG IV still supports RPG III type data structure declaration, some programmers use this way only. However, I will use only D-Spec type declaration only.
The syntax of RPG data structure is as below.
D Name_of_DS DS
D Field1 Length
D Field2 Length2
D Field3 Length3
Example of an RPG IV data structure
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++
D W@Name DS
D First 1 8
D Last 9 14
*
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+
C *Entry PList
C Parm W@Name
C First Dsply
C Last Dsply
C Return
In the example above, the entry parameter is supposed to be concatenation of First and last names of a person.
When we call the program with parameter 'TutorialIndia' we receive the following output.
DSPLY Tutorial
DSPLY India
So, this is an easy way to accomplish functionality similar to %SubSt function !.
However, this is not the only purpose of a data structure. Data structures are found to be more useful for clubbing different data types' variables together.
In the example below, we pass a student's name and marks obtained in two papers. The program will then display the name and average of the marks.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++
D W@Name_Marks DS
D Name 1 8
D Sub1 9 10 0
D Sub2 11 12 0
D Avg S 2 0
*
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++
C *Entry PList
C Parm W@Name_Marks
C Eval Avg = (Sub1 + Sub2) / 2
C Name Dsply
C Avg Dsply
C Return
When I called this program with 'Jones 6896' as parameter, I got the following output
DSPLY Jones
DSPLY 47
Notice that since first 8 spaces of the data structure are declared to be character hence I have to fill the remaining spaces of this portion with blanks. We always have to take care of this thing. Also, since rest of the portion is declared to be numeric, we can not fill characters here. Otherwise we have to face the dreaded decimal data error.
I think, you get the point that the data structure has enabled us to use sub1 and sub2 parameters as different numeric variables and we were able to perform mathematical operations on them!
Initialize a data structure
We can either initialize each part of a data structure separately or put the keyword INZ at the data structure level to initialize each field of the data structure at a time.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++
D W@Ds DS Inz Note: - Since initializing an entry parameter is not allowed, you should not try to initialize the data structures in the two examples given above.
- 11207 reads
