• 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 (And So Did Spring!)

    October 4, 2021 Jon Paris

    When I first sat down to write this tip my focus was on introducing you to the new RPG features that were added alongside the latest September Technology Refresh (TR). However, when I tried to reference one of the enhancements from the spring enhancements, I quickly discovered that I had never written those up for the Guru column!

    Oooopppps! As a result, this “tip” will cover all the RPG enhancements added this year, both in the April and September announcements. It will be in two parts. This first part will cover the array-oriented features.

    RPG’s range of array-processing options has expanded significantly over the years and most of us really don’t use them enough. The second part will cover the other new features in this year’s enhancements, which include other new BIFs and a helpful new addition to RPG debugging facilities.

    This story contains code, which you can download here.

    Array Enhancements

    Let’s start with %SPLIT, which was part of the spring additions. At first glance you may wonder why I include this as an array function. I’m counting it because it produces an array from text input.  For example, if I have a variable textString containing a simple sentence which I want to break up into individual words, I can code this:

    dcl-s  textString  varchar(50)
                       inz('The quick brown fox jumps over the lazy dog');
    dcl-s  words  varchar(20)  dim(*Auto: 20);                   
    
    words = %Split( textString );
    

    The BIF “splits” the input into substrings using the default separator of a space.  As a result, element 1 of the array words now contains “The”, element 2 “quick”, and so on.

    If what you need is to break up a CSV string then you can do that with %SPLIT by using the optional second parameter which can contain one or more separator characters. Like so:

    words = %Split( csvString: ',' ); // Just split at commas
    words = %Split( textString: '.,!? ' );  // Split on any major punctuation 
    

    One warning here. %SPLIT ignores consecutive separators. Most of the time that is a good thing but can present a problem in some circumstances. For example if you were processing a CSV file containing the string ABC,,XYZ using CPYFRMIMPF it would result in three fields. “ABC”, blank, and “XYZ”. However, %SPLIT would only produce two array elements because the second comma would be ignored. So, in these circumstances some pre-processing of the input may be required.

    The second example shows how you could break up a sentence such that most punctuation marks are ignored. Notice that I included a blank at the end of the string so that spaces would count as a separator. If you also wanted to ignore quotation marks etc. you would simply add them to the separator list.

    Don’t forget that, because this is a BIF we’re talking about, it can be used anywhere that a variable of the same type can be used. So, for example, I could simply use %SPLIT as an argument to the FOR-EACH op-code rather than have to define an array within the program.

    for-each  word in %Split( textString );
       // Process the word ...
    EndFor;
    

    %MINARR and %MAXARR

    When %MIN and %MAX were first introduced I was happy to see them, but I thought at the time that it would also be nice to have a variant that operated on arrays rather than a list of values. The problem was that in their original incarnation these BIFs returned the actual value and when dealing with an array one would really want the index of the value. If you are not familiar with these BIFs you can find the basics here in IBM’s announcement.

    %MINARR and %MAXARR fill that gap. %MINARR returns the index of the lowest value in the array and %MAXARR returns the index of the highest. These BIFs can be applied to a conventional array or to a DS array.

    This latter capability is particularly useful. Suppose you have a salesman number and sales volume in a DS array. To identify the poorest performer, you could apply %MINARR to the sales value and use the resulting index to access the salesman’s number and name. Like this:

    dcl-ds  salesData  qualified  dim(*Auto : 100);  // Maximum 100 salesmen
       salesRep#    char(5);
       name         varchar(30);
       salesToDate  packed(9:2);
    end-ds;
    
    index = %minArr( salesData(*).salesToDate );
    Dsply ( 'Rep #' + salesData(index).salesRep# + ' Name: '
            + salesData(index).name );
    

    You can limit the scope of the search by specifying the highest element number to search via the third parameter.  The default is to search the whole array.  As shown in these examples, these days I use automatic arrays for most of my work, so I never have to worry about limiting the scope of the BIF, as RPG does it automatically for me. (If you are not familiar with dynamic arrays you can find details here. If you’re not at V7.4 yet, the only way you can limit the scope of the search is by specifying the highest element number to search in the third parameter.

    In the following example, I am assuming that the variable activeItems contains the actual number of elements in the array invoiceList and I am attempting to locate the invoice with the highest value.

    In case you are wondering about the second parameter, it can be used to specify the starting index if desired. As you would expect, it defaults to a value of 1 when only a single parameter is supplied.

    index = %maxArr( invoiceList(*).invoiceTotal : 1 : activeItems );
    

    SORTA Enhancement

    Some time ago SORTA was enhanced to permit the sorting of DS arrays. Useful as this was, it was limited to sorting on a single key field. This meant that whenever I needed a multi-key sort I still had to resort to using the C function qsort(). Not a problem for me as I have been using it for years, but a potential hurdle for those who come after me and need to maintain my code. With the new support there are very few occasions where I need to use qsort() as RPG now directly supports multi-key sorting.

    This support is provided by using %FIELDS to list the key fields to be used in their priority sequence. In the example below I am sorting the list of animals by weight in type (i.e., cat, dog, etc.). In other words, all the cats will be grouped together and sequenced in order of their weight.

    dcl-ds myAnimals  qualified  dim(99);
       ID      int(10);
       type    varchar(16);
       name    varchar(16);
       weight  packed(7:2);
    end-ds;
    ...
    SortA  myAnimals %Fields( type: weight );
    

    Wrapping Up

    As you can see there are some nice features here that can make our programming lives a little easier. RPG just keeps on growing!

    Before diving into trying these new features don’t forget to check that you have the required PTFs installed. You can find the details here for both the Spring and Fall TRs: https://ibm.biz/rpg_cafe

    In the second part of this tip, I will be looking at the other features that have been added to RPG this year.

    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.

    RELATED STORY

    Guru: Dynamic Arrays Come To RPG

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, BIF, FHG, Four Hundred Guru, IBM i, RPG

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Db2 PTF Group Enhancements Target Web Services, Audit Journal Hit A Fiduciary Home Run With A Backup, DR, Cybersecurity Triple Play

    One thought on “Guru: Fall Brings New RPG Features (And So Did Spring!)”

    • Jose says:
      October 5, 2021 at 8:26 am

      It’s getting better and better!

      Reply

    Leave a Reply Cancel reply

TFH Volume: 31 Issue: 64

This Issue Sponsored By

  • ProData
  • UCG Technologies
  • WorksRight Software
  • ASNA
  • Manta Technologies

Table of Contents

  • Fresche Buys Abacus To Integrate From IBM i To Cloud To Code
  • Hit A Fiduciary Home Run With A Backup, DR, Cybersecurity Triple Play
  • Guru: Fall Brings New RPG Features (And So Did Spring!)
  • Db2 PTF Group Enhancements Target Web Services, Audit Journal
  • For The Prices They Are A-Changin’

Content archive

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

Recent Posts

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

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