RPG/400 Help

LOKUP (Look Up)

*---------*---------------*-----------------*----------------*---------------*
|   CODE  |    FACTOR 1   |     FACTOR 2    |     RESULT     |  INDICATORS   |
|         |               |                 |      FIELD     |               |
*---------*---------------*-----------------*----------------*---------------*
| LOKUP   |               |                 |                |               |
*---------*---------------*-----------------*----------------*---------------*
| (array) |  Search       |    Array name   |                |   HI LO EQ    |
|         |  argument     |                 |                |               |
*---------*---------------*-----------------*----------------*---------------*
| (table) |  Search       |    Table name   |   Table name   |   HI LO EQ    |
|         |  argument     |                 |                |               |
*---------*---------------*-----------------*----------------*---------------*

The LOKUP operation causes a search to be made for a particular element in an array or table. Factor 1 is the search argument (data for which you want to find a match in the array or table named). It can be: a character or numeric literal, a field name, an array element, a table name, a named constant, or a figurative constant.

If a table is named in factor 1, the search argument used is the element of the table last selected in a LOKUP operation, or it is the first element of the table if a previous LOKUP has not been processed. The array or table to be searched is specified in factor 2.

For a table LOKUP, the result field can contain the name of a second table from which an element (corresponding positionally with that of the first table) can be retrieved. The name of the second table can be used to reference the element retrieved. The result field must be blank if factor 2 contains an array name.

Decimal alignment is not processed for LOKUP operations.

Resulting indicators specify the search condition for LOKUP. One must be specified in positions 54 through 59 first to determine the search to be done and then to reflect the result of the search. Any specified indicator is set on only if the search is successful. No more than two indicators can be used. Resulting indicators can be assigned to equal and high or to equal and low. The program searches for an entry that satisfies either condition with equal given precedence; that is, if no equal entry is found, the nearest lower or nearest higher entry is selected.

Resulting indicators can be assigned to equal and low, or equal and high. The LOKUP operation searches for an entry that satisfies either condition with equal given priority.

High (54-55): Instructs the program to find the entry that is nearest to, yet higher in sequence than, the search argument. The first higher entry found sets the indicator assigned to high on.
Low (56-57): Instructs the program to find the entry that is nearest to, yet lower in sequence than, the search argument. The first such entry found sets the indicator assigned to low on.
Equal (58-59): Instructs the program to find the entry equal to the search argument. The first equal entry found sets the indicator assigned to equal on.

When you use the LOKUP operation, remember:

Figure "LOKUP Operation with Arrays" shows examples of the LOKUP operation with arrays.

LOKUP Example

Figure 37. LOKUP Operation with Arrays

*...1....+....2....+....3....+....4....+....5....+....6....+....7...
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComments++++++
C*
C*  In this example, the programmer wants to know which element in
C*  ARY the LOKUP operation locates.  The Z-ADD operation sets the
C*  field X to 1.  The LOKUP starts at the element ARY that is
C*  indicated by field X and continues running until it finds the
C*  first element equal to SRCHWD.  The index value, X, is set to
C*  the position number of the element located.
C                     Z-ADD1         X       30
C           SRCHWD    LOKUPARY,X                    26      EQUAL
C*
C*  In this example, the programmer wants to know if an element
C*  is found that is equal to SRCHWD.  LOKUP searches ARY until it
C*  finds the first element equal to SRCHWD.  When this occurs,
C*  indicator 26 is set on.
C           SRCHWD    LOKUPARY                      26      EQUAL
C*
C*  The LOKUP starts at a variable index number specified by field
C*  X.  Field X does not have to set to 1 before the LOKUP
C*  operation.  When LOKUP locates the first element in ARY equal
C*  to SRCHWD, indicator 26 is set on.  The index value, X, is
C*  set to the position number of the element located.
C*
C           SRCHWD    LOKUPARY,X                    26      EQUAL


[ Top of Page | Previous Page | Next Page | Table of Contents ]