LOOKUP opcode for ARRAY in RPG AS400 |
Lookup opcode
We do use LOOKUP opcode in RPG fixed format to lookup for an element in the array.
Prompt type . . . C Sequence number . . . 0014.00 Level N01 Factor 1 Operation Factor 2 Result 'NAME1' LOOKUP ARRAY1 Decimal Length Positions HI LO EQ Comment 01
Here we provide LOOKUP opcode in the operation field and FACTOR1 will contain the search item and FACTOR2 will have the name of the array.
We do have to use indicators either HI, EQ or LO, EQ, So once the record is found in the array using LOOKUP opcode the %EQUAL and %FOUND will return 1 (successful).
%LOOKUP bif
Lookup opcode is used to search for an element in the RPG Arrays. This opcode will find the exact match of the search argument in the Array.
%LOOKUP function will return the array index of the search argument in the array if found in the array. This function can also be used with Array Data structure but we will not go beyond the scope in this article and will be stuck to the Array lookup.
%Lookup bif parameters
Below are the syntax and parameters required for %LOOKUP opcode. Some parameters are mandatory and some are optional to use.
%lookup(Search Argument:Array:StartIndex:Number of Elements)
Example using %LOOKUP opcode
Let's discuss an example to better understand the %lookup function
Array1 = [5,9,12,50,2]
Search Argument = 12
//array index return = 3 arrayindex = %lookup(12:Array1)
//array index return = 3 arrayindex = %lookup(12:Array1:2)
//array index return = 0 as start searching from 4th index of the ARRAY1 arrayindex = %lookup(12:Array1:4)
//array index return = 0 as start searching from 1st index till 2nd index of the ARRAY1 arrayindex = %lookup(12:Array1:1:2)
//array index return = 3 as start searching from 2nd index till 3rd index of the ARRAY1. arrayindex = %lookup(12:Array1:2:2)
RPGLE code for lookup an array element in Fixed, Free, and Fully Free format
*Header Specification HDebug(*Yes) HOption(*NoDebugio) * program variables D Array S 10A DIM(5) * D Index S 10i 0 D search S 10a * C EVAL SEARCH = 'NAME1' C EVAL ARRAY(1) = 'NAME1' C EVAL ARRAY(2) = 'NAME2' C EVAL ARRAY(3) = 'NAME3' C EVAL ARRAY(4) = 'NAME4' C EVAL ARRAY(5) = 'NAME5' C SEARCH LOOKUP ARRAY 01 C IF *IN01 = *ON C 'FOUND' DSPLY C ENDIF C SETON LR
*Header Specification HDebug(*Yes) HOption(*NoDebugio) * program variables D Array S 10A DIM(5) * D Index S 10i 0 * /Free // Begin program Array(1) = 'NAME1'; Array(2) = 'NAME2'; Array(3) = 'NAME3'; Array(4) = 'NAME4'; Array(5) = 'NAME5'; // %lookup(Search Argument:Array:StartIndex:Number of Elements) index = %lookup('NAME3':Array); DSPLY Index; //index = 3 index = %lookup('NAME4':Array); DSPLY Index; //index = 4 index = %lookup('NAME2':Array:3); DSPLY Index; //index = 0 index = %lookup('NAME5':Array:2:2); DSPLY Index; //index = 0 //Set Last Record Indicator ON *Inlr = *ON; /End-Free
**FREE ctl-opt debug(*yes); ctl-opt Option(*NoDebugio); dcl-s Array char(10) DIM(5); dcl-s Index int(10); // Begin program Array(1) = 'NAME1'; Array(2) = 'NAME2'; Array(3) = 'NAME3'; Array(4) = 'NAME4'; Array(5) = 'NAME5'; // %lookup(Search Argument:Array:StartIndex:Number of Elements) index = %lookup('NAME3':Array); DSPLY Index; //index = 3 index = %lookup('NAME4':Array); DSPLY Index; //index = 4 index = %lookup('NAME2':Array:3); DSPLY Index; //index = 0 index = %lookup('NAME5':Array:2:2); DSPLY Index; //index = 0 //Set Last Record Indicator ON *Inlr = *ON;
Related Post
- Array and Types of Array in RPG AS400
- Compile Time Array in RPG AS400
- Using PERRCD keyword in compile Time Array in RPG AS400
- Run Time Array in RPG AS400
- Pre-Run Time Array in RPG AS400
- Sorting Array (SORTA) opcode for ARRAY in RPG AS400
- XFOOT opcode for ARRAY in RPG AS400
- Using keywords QUALIFIED, LIKEDS, and DIM with data structures
- Array Data Structures in RPG AS400