Now, Get RPG Codes in all three formats only on this website on every RPGLE related programming article.

Fixed, /Free and Fully Free RPG


Lookup an Array element in RPGLE AS400

LOOKUP an array element in RPGLE AS400
LOOKUP opcode for ARRAY in RPG AS400, lookup, lookup array, array, lookup an array element in rpgle,lookup opcode,%lookup bif,LOOKUP opcode in rpgle, array opcode, rpg opcodes,what,about, introduction, how,why
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)
  • Search argument is the item being searched in the array.
  • The array is the name of the array in which the search has to be done.
  • Start Index is optional and if it is passed then the search started from the start index position of the array. For Example, if the Start index is 5 and Array has a total of 10 elements stored then the lookup will cause the search to start from the 5th index and forward instead of the 1st index.
  • The number of Elements is the number of next array indexes to be searched from the start index in the array. For Example, if the start index is 2, and number of elements is 3 and the array has a total of elements of 10 then the search will be done only on the 2nd,3rd, and 4th elements of the array
  • Example using %LOOKUP opcode

    Let's discuss an example to better understand the %lookup function

  • Take an Array of 5 elements as follows named ARRAY1
  • Array1 = [5,9,12,50,2]
    
  • Let's search for 12  in ARRAY1
  • Search Argument = 12
  • Let's discuss various ways to use %LOOKUP opcode and what it returns?
  • //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

    RPG Code in Fixed format for Lookup an Array element in RPGLE AS400
          *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                                                    
    
    RPG Code in /Free format for Lookup an array element in RPGLE AS400.
     *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                                                                                                                             
    
    RPG Code in fully Free format for lookup an array element in RPGLE AS400
    **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

    Post a Comment

    © AS400 and SQL Tricks. All rights reserved. Developed by Jago Desain