• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Fall Brings New RPG Features, Part 2

    January 11, 2021 Jon Paris

    In my previous tip I outlined some of the new features added to RPG with the Fall 2020 release. In this and the following tip I will be covering the features that I ran out of space for in that first one.

    This time I will cover the new FOR-EACH loop construct. I have wanted this in RPG ever since encountering it in PHP. Simply put, it automatically iterates through an array, “serving up” one element at a time. When I say “an array” I mean any kind of array, including data structure arrays, dynamic arrays and even the new temporary arrays created by the %LIST BIF that I mentioned in the previous tip.

    Here’s a really simple example of using FOR-EACH to process all of the elements in a simple standalone array:

    dcl-s  nameArray  char(10)  Dim(100);
    dcl-s  name       like(nameArray);
    
    // The old way
    For element = 1 to %elem(nameArray);
       Dsply  nameArray(element);
    EndFor;
    
    // The For-Each way
    For-Each  name in nameArray;
       Dsply name;
    EndFor;
    

    Notice that because FOR-EACH extracts each element into the name field, we don’t need to use a subscript to access the element within the loop. Not that big a deal when processing a simple array, but it makes life a lot simpler when dealing with a multi-field DS array. You’ll see an example of this in a moment.

    Where FOR-EACH really shines, though, is when working with the new dynamically sized arrays. Because RPG knows how many active elements are in the array, it is able to use that information to control the operation of the loop.

    One of my favorite uses of the new dynamic array support is for handling SQL result sets, so I’ll use SQL for this next example. What I love is that the combination of the dynamic array and FOR-EACH means that I don’t actually have to use the SQLERRD(3) value to control the loop. Here’s my simple example.

    Dcl-Ds  results  ExtName('ANIMALS')  qualified  dim( *Auto : 1000 ) End-ds;
    Dcl-Ds  animal  likeDs(results);
    
    Dcl-s  type  like(results.TYPE);
    
    Dsply ( 'What type of animal do you want?' ) ' ' type;
    
    Exec SQL
         Declare animalsCursor cursor for
            Select * from ANIMALS where type = :type;
    
    Exec SQL
         Open animalsCursor;
    
    Exec SQL
         Fetch from animalsCursor for 1000 rows into :results;
    
    Dsply ( 'Found ' + %Char( %Elem( results ) ) + ' matching animals' );
    
    For-Each animal in results;
       Dsply ( 'Id: ' + %Char(animal.Id) + ' - Name: ' + animal.name );
    EndFor;
    
    

    You can also see here what I meant by simplified field references. Had I coded the loop manually, references such as animal.Id and animal.name would have needed to be coded as results(element).Id and results(element).name. More typing and less elegant.

    For me though, this is not just a reduction in typing. It also increases the readability in the sense that the intent of my code is more obvious, i.e., I am going to process every active element in this array.

    Now that you’ve seen how much easier this combination can make processing SQL just imagine how much better still it would be if we didn’t have to worry about having to use a cursor. Wouldn’t it be nice just to do a multi-row SELECT into the target DS? That would be so much easier and closer to what happens in other languages. It just so happens that I am not the only one who feels this way, and there is an RFE out there requesting that IBM provide this functionality. IBM do pay a lot of attention to the number of votes that requests like this receive so, if you agree with me, won’t you join me in voting for this enhancement? You’ll find it at https://www.ibm.com/developerworks/rfe/execute?use_case=viewRfe&CR_ID=141665.

    Is FOR-EACH A Good Fit For All Your Array-Processing Needs?

    As you have seen, FOR-EACH works beautifully when processing all the elements of an array and in particular, when processing a dynamic array. But what about a traditional (i.e., fixed-sized) RPG array that has not been filled? Prior to the advent of FOR-EACH we would normally code this in a regular FOR loop, using the highest active element number as the upper limit. We can use FOR-EACH in such situations, but it requires that we use %SUBARR to limit the scope of the array and the resulting code is not quite so elegant. As a result, I suspect I may still use the old approach in such situations, but take a look at the comparison example below and form your own opinion.

    // Traditional FOR loop approach
    For element = 1 to nameCount;
       Dsply nameArray(element);
    EndFor;
    
    // Using FOR-EACH with a partially filled array
    For-each name in %Subarr(nameArray: 1: nameCount);
       Dsply name;
    Endfor;  
    

    Next Time

    In my third and final tip on these enhancements, I will be taking a look at a couple of new compiler options designed to aid in conversions from character to numeric. You’ll see that not only do they improve the operation of BIFs such as %DEC, but also aid in processing XML and JSON.

    Jon Paris is one of the world’s foremost experts on programming on the IBM i platform. A frequent author, forum contributor, and speaker at User Groups and technical conferences around the world, he is also an IBM Champion and a partner at Partner400 and System i Developer. He hosts the RPG & DB2 Summit with partners Susan Gantner and Paul Tuohy.

    RELATED STORIES

    Fall Brings New RPG Features

    7.4 Brings New RPG Goodies

    Guru: Dynamic Arrays Come To RPG

    Dynamic Arrays Come To RPG – The Next Part Of The Story

    Dynamic Arrays Come To RPG – Limitations, Circumventions, And More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, dynamically sized arrays, FHG, Four Hundred Guru, IBM i, JSON, RFE, RPG, XML

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    More Vintage Power Systems Feature Withdrawals Thoroughly Modern: DevOps Refactoring Of RPG Applications with RDi

    3 thoughts on “Guru: Fall Brings New RPG Features, Part 2”

    • Rick says:
      January 11, 2021 at 8:44 am

      Couldn’t find your RFE 141665 to vote. Great article!

      Reply
    • sAM says:
      January 11, 2021 at 12:19 pm

      I could not get this link to work:

      IBM do pay a lot of attention to the number of votes that requests like this receive so, if you agree with me, won’t you join me in voting for this enhancement? You’ll find it at https://www.ibm.com/developerworks/rfe/execute?use_case=viewRfe&CR_ID=141665.

      Reply
    • Sam says:
      January 11, 2021 at 12:43 pm

      I found the RFE by searching by the id, 141665. However, it is marked as “Declined”.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 31 Issue: 2

This Issue Sponsored By

  • Profound Logic Software
  • Fresche Solutions
  • CYBRA
  • Computer Keyes
  • WorksRight Software

Table of Contents

  • Seiden Group Unveils A PHP Distro For IBM i
  • Thoroughly Modern: DevOps Refactoring Of RPG Applications with RDi
  • Guru: Fall Brings New RPG Features, Part 2
  • More Vintage Power Systems Feature Withdrawals
  • IBM i PTF Guide, Volume 23, Number 1

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle