HP 3000 Manuals

Using Tables and Arrays [ HP RPG/XL Programmer's Guide ] MPE/iX 5.0 Documentation


HP RPG/XL Programmer's Guide

Using Tables and Arrays 

Tables and arrays are the most convenient and efficient way to organize
elements of related data so that you can reference them individually or
as a group.  Each element in a table or array has the same length, the
same data type (numeric or alphanumeric) and, if numeric, the same number
of decimal positions.  Tables and arrays are defined using the File
Extension Specification.

You commonly use tables to keep rates, constants or data of any kind that
remains the same during program execution.  For instance, you can enter a
table for state tax rates.

Normally, you use arrays to save data accumulated during program
execution.  However, arrays and tables can often be used interchangeably.

Differences Between Tables and Arrays 

Tables are loaded either during program compilation or before a program
is executed.  Tables can only be processed one element at a time; you
cannot perform operations on an entire table in one operation.  Table
names start with TAB. When you use a table name, RPG selects the table
element found during the last lookup (LOKUP) operation.

Arrays are loaded either during compilation, or before or during program
execution.  You can process arrays one element at a time.  You can also
process all elements of an array in one operation by using the array
name.

Kinds of Tables and Arrays 

There are three kinds of tables and arrays:  compile-time,
preexecution-time and execution-time.  They are grouped into these
categories according to when they are loaded or created by a program.

The kind of table or array that you use depends how you're using it in
your program:

This kind of                 Is used when: 
table or array: 

Compile-Time                 The table or array elements change infrequently, if at
                             all.

Preexecution-Time            The table or array elements change frequently.

Execution-Time               The array elements change frequently or they are created
                             by the program.

Compile-time, preexecution-time and execution-time tables and arrays are
discussed in the next three sections.

Compile-Time Tables and Arrays.  Compile-time table and array values are
loaded during program compilation.  These values can be kept as part of
the source program or they can be saved in separate files on disc.  Once
compiled, compile-time tables and arrays become a part of the object
program.  To permanently change elements in them, you must recompile the
program.

Figure 5-1 shows what compile-time array values look like when saved in a
disc file.  The name of this particular disc file is FLBL1.  It contains
the function key labels for the program shown in Figure 5-2.
_____________________________________
|                                   |
|     ADD        MODE               |
|     CHANGE     MODE               |
|     INQUIRY    MODE               |
|                                   |
|     DELETE     MODE               |
|                                   |
|                                   |
|     EXIT                          |
|                                   |
_____________________________________

          Figure 5-1.  Compile-Time Array Entries on Disc 

Figure 5-2 shows segments of a program that uses two compile-time arrays,
LBL and MSG (the program from which the segments are taken is listed in
Figure 4-23).  LBL is an array that contains function key labels.  The
labels come from the file, FLBL1, shown in Figure 5-1.  The MSG array
contains messages that are displayed on the terminal during program
execution.  The actual messages for the MSG array follow the Output
Specifications in Figure 5-2.

[]
Figure 5-2. Using Compile-Time Arrays Comments 1 This line defines the LBL array. Columns 33-35 contain 1 to specify that there is one function key label per record in the array file on disc. Columns 36-39 contain 8 to specify the number of elements in the array. Columns 40-42 contain 16 to specify the length of each element in the array. 2 This line defines the MSG array. Columns 33-35 contain 1 to specify that there is one message per record in the array. Columns 36-39 contain 20 to specify the maximum number of elements in the array. Columns 40-42 contain 79 to specify the length of each element in the array. 3 This line enables the function keys and displays their labels from the LBL array. 4 This operation sets the value of the index (M) in the MSG array to 5. 5 This line enters the VPLUS action, SHOMSG, into the output field, ACTION. 6 This performs exception output for the record associated with EXCPT Group V$MESG. 7 This line begins the description of the output record used for displaying VPLUS messages. 8 This line specifies that the fourth field in the output record is an element (selected by M) from the MSG array. 9 This line is a File Name record. It identifies the disc file (FLBL1) which contains values for the LBL array. The contents of FLBL1 are loaded and compiled as part of the program. Figure 5-1 shows the contents of this file. The MSG array entries follow this line. Preexecution-Time Tables and Arrays. Preexecution-time tables and arrays are loaded before a program begins the RPG program cycle. Elements in these tables and arrays are available before files are read and before calculations are performed. Preexecution-time tables and arrays can reside on the same disc device or on different disc devices. They are loaded in the same order as their File Extension Specifications are listed. Table 5-1 is a table of postal rates that is used to calculate item shipping charges. (Only the first 8 and the last 3 entries are shown. Since the last entry is 30 pounds, the items are assumed to weigh no more than 30 pounds.) The postal charges are based on an item's weight, with fractions of pounds putting the item in the next higher weight category. Table 5-1. Weights/Postal Charges --------------------------------------------------------------------------------------------- | | | | Weight (lbs.) | Postal Charges | | | | --------------------------------------------------------------------------------------------- | | | | 1 | $0.45 | | | | --------------------------------------------------------------------------------------------- | | | | 2 | .50 | | | | --------------------------------------------------------------------------------------------- | | | | 3 | .50 | | | | --------------------------------------------------------------------------------------------- | | | | 4 | .55 | | | | --------------------------------------------------------------------------------------------- | | | | 5 | .55 | | | | --------------------------------------------------------------------------------------------- | | | | 6 | .55 | | | | --------------------------------------------------------------------------------------------- | | | | 7 | .60 | | | | --------------------------------------------------------------------------------------------- | | | | 8 | .65 | | | | --------------------------------------------------------------------------------------------- | | | | . | . | | | | --------------------------------------------------------------------------------------------- | | | | . | . | | | | --------------------------------------------------------------------------------------------- | | | | . | . | | | | --------------------------------------------------------------------------------------------- | | | | 28 | 1.00 | | | | --------------------------------------------------------------------------------------------- | | | | 29 | 1.05 | | | | --------------------------------------------------------------------------------------------- | | | | 30 | 1.05 | | | | --------------------------------------------------------------------------------------------- Figure 5-3 shows the contents of the postal rates table as it is saved on disc. The name of this particular disc table file is WGHRATE. It is used in the program shown in Figure 5-4. _____________________________________ | | | 010045 | | 020050 | | 030050 | | 040055 | | 050055 | | 060055 | | 070060 | | 080060 | | . | | . | | . | | 280100 | | 290105 | | 300105 | | | _____________________________________ Figure 5-3. Preexecution-Time Table Entries on Disc Figure 5-4 shows program segments that look up postal rates contained in the file, WGHRATE (see Figure 5-3). The program uses the rates to calculate invoice shipping charges. The program first reads an item from the ORDER file. It then searches the WGHRATE table (line 7) to find a weight less than or equal to the item's WEIGHT. When a weight is found, the corresponding rate is moved to the invoice record field, TABRAT.
[]
Figure 5-4. Using a Preexecution-Time Table Comments 1 This line defines the ORDER file containing the items to be printed on the invoices. 2 This line defines the WGHRATE table file on disc. The File Extension Specification on line 4 describes the table in detail. 3 This line defines the INVOICE file. It is a report file assigned to the printer. 4 This line defines the WGHRATE table. Columns 11-18 contain WGHRATE to identify the table file. WGHRATE is loaded into the program before it is executed. Columns 27-32 contain TABWGT to define the weight table. Columns 33-35 contain 1 to specify that there is one weight table entry and one rate table entry per record. Columns 36-39 contain 30 to specify that there are 30 elements in each table. Columns 40-42 contain 3 to specify that the length of each weight table element is 3 positions. Column 44 contains 1 to specify that each weight table element has one decimal place (fractions of a pound are possible). Columns 46-51 contain TABRAT to define the rate table as an alternating table. Columns 52-54 contain 3 to specify the length of the rate table element. Column 56 contains 2 to specify that each rate table element has 2 decimal places. 5 This line begins the description of record 01 in the ORDER file. 6 This line defines the WEIGHT field in the ORDER file. This field will be used as the search field to find the correct postal rate in the WGHRATE table. 7 This line specifies a LOKUP operation which searches the WGHRATE table to find the postal rate corresponding to each item's weight. Columns 10-11 contain 02 to specify that this operation is performed when indicator 02 is turned on (assume that indicator 02 is turned on when an item requires postal delivery). Columns 18-27 contain WEIGHT to specify the search field. Columns 28-32 contain LOKUP to specify the table look-up operation. Columns 33-42 contain TABWGT to specify the table field to be searched. Columns 43-48 contain TABRAT to specify the table field to be selected when WEIGHT is equal to or less than TABWGT. Columns 56-59 contain 2323 to specify that indicator 23 is turned on when a weight is found in TABWGT equal to or less than WEIGHT. 8 This line defines the INVOICE record that prints the postal rate found by a successful search of the WGHRATE table. The rate table name, TABRAT, selects the element in the table (not the entire table) found by the last LOKUP operation. Execution-Time Arrays. Execution-time arrays are loaded from files named in File Description and File Extension Specifications. They can also be created by Calculation Specifications. There are no execution-time tables. Figure 5-5 lists a program that uses an execution-time array, AR. This program is an online program that lets users query an employee master file by department number. When the department number is found, the last name of each employee in the department is saved in the AR array. When the array is filled with ten names, they are displayed on the terminal.
[]
Figure 5-5. Using an Execution-Time Array Comments 1 This line defines the MASTFL file containing employee records. 2 This line defines the TERMINAL file that is processed in line (character) mode. 3 This line defines the array AR. It is used to store the last names of employees matching a selected department number. Columns 27-32 contain AR which is the name of the last names array. Columns 36-39 contain 10 to specify that there are 10 elements in the array. Columns 40-42 contain 29 to specify that each element in the array is 29 characters long. 4 This line begins the input record description for the employee master file, MASTFL. 5 This line begins the input record description for the terminal file, TERMINAL. 6 This line reads a department number entered from the terminal. 7 This line reads a record (for the department) in the employee master file, MASTFL. 8 This line increments the index, I, to the AR array. (An employee record matched the department entered by the user.) 9 This line compares the AR index I to its maximum value. If the array is full, indicator 21 is turned on. 10 This line begins the output record description for the teminal file, TERMINAL. This record is displayed when indicator 21 is turned on. 11 This line specifies that the second field to be displayed in the TERMINAL output record is contained in the AR array.


MPE/iX 5.0 Documentation