Tuesday, January 26, 2021

RPG's new Built-in Function %LIST and New Operation Code FOR-EACH - IBM i


RPG is the primary programming language for development on IBM i. And, IBM continues to provide enhancements to RPG. 

In this post, we will see couple of recent additions to RPG. 
  • Built-in Function %LIST
  • Operation Code FOR-EACH
Click Here to read my previous post on new BIF %RANGE and Operator IN. 


New Built-in Function %LIST provides an easy way to populate the data into an array. %LIST accepts list of values and returns a temporary array.

%LIST can be directly used where arrays are allowed. This provides an option to developer to use %LIST instead of having to create an array where ever possible. There are some exceptions to this, %LIST cannot be used with SORTA (Sort an Array), %LOOKUP and %SUBARR. 


%LIST(item1 : item2 : item3 : ...) 

Below are some of the important points to note while working with %LIST. 
  • Minimum of one operand to be passed. 
  • All Operands must be of same type (like, %LIST with only numeric values or %LIST with only Character values. Not both the types at the same time). 
  • Arrays or Data structures cannot be passed as operands.
  • If assigning data to an array, Defined array should be of same data type as the data passed. 
%LIST can be used in the new FOR-EACH loop or in IF conditions using new Operator IN. 

FOR-EACH (For Each)

New Operation code FOR-EACH provides an easier way to iterate through an array, Subarray (using %SUBARR) or temporary array (using %LIST).

FOR-EACH Opcode begins a loop and ENDFOR to end the loop. 


FOR-EACH(H) item IN Array(or %SUBARR or %LIST)

Below are some of the important points to note while working with FOR-EACH.
  • Extender 'H' can be used to half adjust the numeric values.
  • First Operand cannot be an array. 
  • Data type of the first operand should match with the data type of an array, sub array or list. 
  • First operand can be a data structure. But, second operand should be related to the first operand by using LIKEDS.
We will see an example using both %LIST and FOR-EACH to understand better. 

New BIF %LIST and Opcode FOR-EACH in RPGLE - IBM i

In the above example, 
  • Line - 8: %LIST with the list of colours (character) returns a temporary array and assigns the data to the array already created. 
  • Line - 13: FOR-EACH Opcode would loop through the array 'wFavoriteColours' (second operand). Loop is repeated for each element in the array and data can be accessed using 'wColour' (first operand) inside the loop. Variable used in the first operand needs to be defined already. 
  • Line - 17: FOR-EACH Opcode would loop through the list of values provided using %LIST (with out having to define array). 
  • Line - 22: FOR-EACH Opcode would loop through the list of numeric values provided using %LIST. 
  • Line - 28: FOR-EACH Opcode would loop through the part of the array using %SUBARR. 
We are only using arrays in the above example. Below is a simple example showing how FOR-EACH loop can be used with Data structures. 

FOR-EACH loop with Data structures in RPGLE - IBM i

Hope the above info was a bit of help to understand %LIST and FOR-EACH better. 

If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

No comments:

Post a Comment

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, dat...