Here are the most commonly used Array Built-In Functions (BIFs) in free-form RPGLE—these are powerful tools for working with arrays efficiently.
✅ Array Built-In Functions (BIFs) in RPGLE
BIF | Description | Example |
---|---|---|
%ELEM |
Returns the number of elements in the array | %ELEM(MyArray) |
%LOOKUP |
Finds the position of a value in an array | %LOOKUP('Paris': Cities) |
%LOOKUPGT |
Finds position of first greater-than value in sorted array | %LOOKUPGT(100: Scores) |
%LOOKUPGE |
Finds position of first greater-or-equal value | %LOOKUPGE(90: Scores) |
%LOOKUPLT |
Finds position of last less-than value | %LOOKUPLT(50: Scores) |
%LOOKUPLE |
Finds position of last less-or-equal value | %LOOKUPLE(50: Scores) |
%TLOOKUP |
Searches a table array using alternate key array | %TLOOKUP('B200': KeyArray: ValueArray) |
%SUBARR |
Creates a sub-array | %SUBARR(MyArray: 2: 3) — elements 2,3,4 |
%TRIM |
Removes spaces from CHAR array values | %TRIM(NameArray(i)) |
🔍 Example Using Key BIFs
**FREE
Dcl-S Cities Char(20) Dim(5) Inz('Paris':'London':'Rome':'Berlin':'Madrid');
Dcl-S Position Int(5);
Dcl-S Scores Packed(5:0) Dim(5) Inz(10:20:30:40:50);
// Basic usage of %ELEM
Dsply ('Total cities: ' + %Char(%ELEM(Cities)));
// Using %LOOKUP to find 'Rome'
Position = %LOOKUP('Rome': Cities);
If Position > 0;
Dsply ('Found Rome at position ' + %Char(Position));
Endif;
// %LOOKUPGT on sorted numeric array
Position = %LOOKUPGT(25: Scores); // Finds first value > 25, i.e., 30
Dsply ('First score > 25: ' + %Char(Scores(Position)));
🧠 When to Use Each
-
Use
%LOOKUP
when working with unsorted arrays for exact matches. -
Use
%LOOKUPGT
,%LOOKUPLT
, etc., when arrays are sorted (e.g., ASCEND). -
Use
%SUBARR
for slicing arrays dynamically. -
Use
%TLOOKUP
when searching a key-value pair array structure.