You are on page 1of 23

RPG/400

Session 9

INTRODUCTION
Arrays and tables allow program to
organize groups of related data elements
for easy retrieval.
Data structures allow to define an area of
space within memory to store individual
elements of data.
Elements are subfields within a larger
structure.

TABLES
Store fixed set of data elements for lookup
Generally used when fixed number of possible elements
are in the list.
Used for validation
To extend usefulness of tables RPG allows to define
second table related to the first.
A related table is a list of elements associated with
corresponding element with the primary table.
Tables cab be loaded at Compile time or at pre-runtime.
Compile time tables are loaded with data included in the
source code.
Pre runtime tables load from the Database file during
initilization phase of the program execution.
Table names begin with TAB.

Arrays

Offers more flexibility in


function than table
Program can dynamically
reference and manipulate
individual elements during
execution
One common use is to hold
summary totals during
execution
Arrays can be sorted and
numeric values can be totaled
using single op-code.
Arrays are loaded at compile
time,pre-runtime and runtime.

Compile time arrays are loaded with data


included in the source code.
Pre run time arrays load from the
database file during initialization phase of
program execution.
Runtime arrays are loaded within the
program logic during execution.
Array elements can be accessed using an
index.

Compile Time table


Definition
D TABNAMES

20

DIM(5)
CTDATA
PERRCD(1)
Each element declared as 20 character length
DIM keyword specifies maximum number elements
in table as 5
CTDATA keyword indicates table is loaded using
compile time data embedded in the source.
PERRCD indicates how many elements in table are
loaded from each line of source

DTABSCORE
S
ALT(TABNames)

3 0

DIM(5)

This Defines related table to store test scores.


Declares each element as 3 digit numeric values with zero
decimals.
The ALT keyword declares table as related or alternate table
of TABNames.
You cannot specify PERRCD of CTDATA when ALT keyword
is used.
Data used to load alternate table is loaded from the same
source record as the main table.
The bottom of the source member contain the data used to
load the table

**CTDATA TABNames
RAJ
KIRAN
JOE
SMITH
ANIL
KAPIL
NIKHIL

090
080
075
039
065
082
031

Entry begins at column 1 and optionally


includes name of the table to which data
belongs. It is for readability as well.
If table name is omitted the order in which ou
define tables must follow the content
sequence as well.

C MyName LOOKUP
C
IF
C
EXCEPT
C
ENDIF

TABNames TABScores
*IN90 = *ON
PrintIt

The Student name to be located is in Factor 1 for


lookup operation
The lookup is performed on table TabNames
If Lookup succeeds indicator 90 is seton and
corresponding element TABscore is returned.
The related table TABSCORE contains the
corresponding three digit TESTSCORE value.

90

PRERUNTIME TABLE
FTESTFILE IT F 22
DISK
Table file is identified by entry T in the Col position 18
File is opened for input only.
DTABNames
S 20
DIM(50)
PERRCD(1)
FROMFILE(TestFile)
The name of the table,size and attribute of each
element, maximum number of elements are defined in
the D-Spec
PERRCD keyword defines how many elements of
table to load from each line of file records. Default is 1.
FROMFILE keyword indicates that the table is to be
loaded using records in the file TESTFILE

D TABSCORE S

3 0 DIM(50)
ALT(TABNames)
EXTFMT(P)
Alternate table is declared
Because test score in the file is define as
packed numeric the external format
keyword is used to map the necessary
translation
PERRCD and FROMFILE cannot be
specified with the definition of the ALT
table.

Compile Time Array


D NAMES

20

DIM(5)
CTDATA
PERRCD(1)
DIM indicates capacity of the array
CTDATA indicates it is a compile time array and
records are loaded from bottom of the file.
PERRCD defines how many records to be
loaded from each line of source.

**CTDATA Name
John
Smith
Ajay
Joe
**CTDATA TestScore
085
035
050
091
Each CTDATA begins in column 0
Data is loaded seperately and not relatively as in the
case of tables here as it can be related using index.

EVAL

x=1

Myname

LOOKUP
Name(x)
90
IF
*IN90 = *ON
XFOOT
TESTSCORE TOTSCORES
EXCEPT Printit
ENDIF
Purpose of index
Index sets the starting point for lookup to begin
If lookup is successful index contains the element number where
lookup value is found.

XFOOT offers quick way to add each element together until


total is derived from the end.
Field name specified in the result field holds the sum of the
array elements.

Pre-Runtime Array
F TESTFILE CT F
D NAME S
20

22

DISK
DIM(50)
PERRCD(1)
FROMFILE(TestFile)
TOFILE(TestFile)
D TESTSCORE S 30 DIM(50)
ALT(Name)
EXTFMT(P)
File to be declared in combined mode as records are
changed during execution.
The values in the file are changed after the program
exits.

DX
S 3
0
D Myname S
20
INZ(Joe)
D TOTScore S
3
0
C EVAL
x=1
C Myname LOOKUP
Name(x)
90
IF *IN90 = *ON
EVAL
TESTSCORE = 99
XFOOT TESTSCORE TOTSCORES
EXCEPT Printit
ENDIF
EVAL *INLR = *ON
RETURN
OQPRINT Printit1
Name(x) 25
TestScores
Z
30
38 TOTAL:
TOTSCORES
Z
42

Run Time Arrays


The example uses two arrays holding
names and associated scores.
The program prints the original sequence
The name array is sorted in ascending
order and printed
The score array is sorted in ascending
order and printed.
The example illustrates runtime overlaying
of arrays.

FTESTFILE IF E
FQPRINT

D
DS
DSTUDENTS
D Name
DTestScore
D
Dx
S
DArraySize S

F 132

DISK
PRINTER

23 DIM(5)
20 OverLay(Students:1)
3 0 overlay(Students:21)
Inz
30
3 0
INZ(%Elem(Students))

*--------------------------------* Load The Arrays


*--------------------------------C
DOU
*In90 = *On
C
Read
StRec
C
If
*In90 = *off
C
Eval
x=x+1
C
If
x <= ArraySize
C
Eval
Name(X) = STNM
C
Eval
TestScore(X) = STSC
C
EndIf
C
EndIf
C
EndDo
*-----------------------------------------

90

*----------------------------------------*Print Array Contents in Original Sequence


*----------------------------------------C
Except Head1
C
Do
ArraySize X
C
Except PrintIt
C
EndDo
*-----------------------------------* Sort Arrays By Name And Reprint
*-----------------------------------C
Sorta Name
C
Except Head2
C
Do
ArraySize x
C
ExCept PrintIt
C
EnDDo
*------------------------------------

*-----------------------------------* Sort Arrays By TestScore And Reprint


*-----------------------------------C
Sorta TestScore
C
Except Head3
C
Do
ArraySize x
C
ExCept PrintIt
C
EnDDo
C
C
Eval
*InLR = *On
C
Return

O*
OQPRINT
O
O
E
O
O
O
O
O
O
O

Head1 1

01
30 '-- Not Sorted --'

Head2

01
30 '-- Sorted by Name --'

Head3

01
31 '-- Sorted By Score -

PrintIt
1
Name(X)
TestScore(X)

25
30