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.
It’s getting better and better!