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
Basic idea behind using data structures in RPG is to treat and use different portions of a single variable as different variables.
The syntax of RPG data structure is as below.
D Name_of_DS DS
D Field1 Length
D Field2 Length2
D Field3 Length3
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!
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.
Recent comments
5 days 1 hour ago
5 days 2 hours ago
5 days 2 hours ago
5 days 4 hours ago
5 days 15 hours ago
5 days 17 hours ago
5 days 18 hours ago
5 days 19 hours ago
6 days 8 hours ago
6 days 18 hours ago