%LookUp

650 Views


%LOOKUP (Look Up an Array Element)

 

/FREE
   arr(1) = 'Cornwall';
   arr(2) = 'Kingston';
   arr(3) = 'London';
   arr(4) = 'Paris';
   arr(5) = 'Scarborough';
   arr(6) = 'York';

   n = %LOOKUP('Paris':arr);
   // n = 4

   n = %LOOKUP('Thunder Bay':arr);
   // n = 0 (not found)

   n = %LOOKUP('Kingston':arr:3);
   // n = 0 (not found after start index)
dcl-ds Status_Array  Qualified;        
  Status_Code   char(4) dim(50);       
end-ds; 
                               
// Get number of array elements used                 
   arraycount = %len(%trim(Status_Array)) / 4; 
    
// Sort array, moving blanks to the bottom                        
   sorta %subarr(Status_Array.Status_Code: 1: arraycount); 
     
  index = %lookup(JobSts: Status_Array.Status_Code: 1: arraycount);
                                             
If index = 0;     
  return = *on;                                 
else;                                             
  return = *off;                                
endif;                                            

An array data structure can be sorted using the SORTA (Sort an Array) operation code.
The array can be sorted using one of the subfields as a key, by specifying the DS(*).KEY syntax.
The array can be sorted using more than one subfield as a key, by specifying %FIELDS to list the required subfields.

Dcl-Ds TokenDS  qualified   dim(81);      
   Name     Char(8);                         
  Value     Char(14) ; 
End-Ds;
  
//  Load Field Names into Token.Name                  
For x = 1 to %elem( TokenDS ) ;                       
  TokenDS(x).Name = %Subst( fldNamArr(x): 3 ) ;       
EndFor ;     
                                          
TokenDS(y).Value = RunDat ;            
   
 //  Lookup Token Name to get Token Value      
x = %lookup( r5tkName: TokenDS(*).Name ) ;    
 //  If lookup successful ...                  
If  x > 0 ;    
    tkVal = %trim( TokenDS(x).Value ) ;                           
EndIf; 

Lookup in array with many keys. 

Dcl-DS ds_TEL_Key  QUALIFIED INZ;                            
      Cat   like(X1AACD) ;                                
      Acc   like(X1ACCD) ;                                
      Gig     like(X1ACCD) ;                                
End-DS ds_TEL_Key;                                           
                                                              
Dcl-DS ds_TEL_Array   Qualified INZ;                         
      TEL_Key       char(39)      Dim (999) ;     
      TEL_Cat     like(X1AACD)  Overlay(TEL_Key);           
      TEL_Acc      like(X1ACCD)  Overlay(TEL_Key:*Next);     
      TEL_Gig     like(X1ACCD)  Overlay(TEL_Key:*Next);     
End-DS gds_TEL_Array;                       
                  
ds_TEL_Key.Cat   =  local_Cat_ID;    
ds_TEL_Key.Acc   = local_Acc_ID;     
ds_TEL_Key.Gig    =  local_Gig_ID;  

Count = %Lookup( ds_TEL_Key: ds_TEL_Array.TEL_Key : 1: ElmCnt );    

If count >0;
	//Found;
Else;
   //Not Found;
EndIf;

 

%Lookup and SORTA - IBM.com


FOR i = 1 to %ELEM(families);
   SORTA families(i).children(*).age;
ENDFOR;

SORTA FAMILIES %FIELDS(NUM_CHILDREN : NAME);

When %LOOKUP *NE LOOKUP - rpgmpgm.com

 

Post Comments