Guru: RPG Receives Enumerator Operator
November 11, 2024 Gregory Simmons
The RPG language now (some would argue, finally) has an enumerator operator. This was introduced in IBM i 7.5 TR3 and IBM i 7.4 TR9.
DISCLAIMER: The edibility of the mushrooms listed in my test program are purely to give the program some context and make it interesting. Their edible status is based on information from the Missouri Department of Conservation’s publication A Guide to Missouri’s Edible and Poisonous Mushrooms. Before foraging for or consuming any wild mushroom do your research and be safe.
Let’s dive right into a code sample. We’ll start with a simple linear-main program. These days, when I’m not writing my RPG in a module for a service program, it’s always in a linear-main program. And always start with **Free.
**Free Ctl-Opt Main(enum_test) ActGrp(*Caller); Dcl-Proc enum_test; Dcl-Enum edible_mushrooms Qualified; morel 'Morel'; chanterelle 'Chanterelle'; portobello 'Portobello'; chicken_of_the_woods 'Chicken of the Woods'; End-Enum; Dcl-s mushroom Char(20); Dsply ('Edible mushrooms:'); For-each mushroom in edible_mushrooms; Dsply (mushroom); EndFor; Return; End-Proc enum_test;
In the above example, I’ve defined an enumerator ‘edible_mushrooms’, which I made qualified and includes four mushroom names that are presumed to be edible. While the qualified keyword is not required, I believe enums (like data structures) should always be defined with the qualified keyword. I feel letting other readers know exactly which construct (enum, data structure or array) is being referred to adds to the readability of my code – something I consider essential.
I have also implemented a for-each loop to cycle through each ‘mushroom’ in the ‘edible_mushrooms’ enum. Within this loop, I display each of the edible mushrooms as a simple example, but I hope you see the power inherent in combining enums and for-each operations.
When I run the above program, the output is:
Note that when you use mushroom in an operation, in this case the DSPLY operation, it utilizes the text assigned to each enumerator in the collection.
Next, let’s explore a couple of ways to test if a single value is within the enumeration:
**Free Ctl-Opt Main(enum_test) ActGrp(*Caller); Dcl-Proc enum_test; Dcl-Enum edible_mushrooms Qualified; morel 'Morel'; chanterelle 'Chanterelle'; portobello 'Portobello'; chicken_of_the_woods 'Chicken of the Woods'; End-Enum; Dcl-s mushroom Char(20) Inz('False Morel'); If mushroom in edible_mushrooms; Dsply (%Trim(mushroom) + ': This mushroom is edible'); Else; Dsply (%Trim(mushroom) + ': This mushroom is NOT edible'); Endif; Return; End-Proc enum_test;
In the above example, I’ve coded a simple If statement and a display action if the value of mushroom is and is not in the enumeration. Running this program produces:
Finally, here’s one more example with a Select statement. If you were not already aware, the RPG Select operation got some sweet upgrades a while back.
**Free Ctl-Opt Main(enum_test) ActGrp(*Caller); Dcl-Proc enum_test; Dcl-Enum edible_mushrooms Qualified; morel 'Morel'; chanterelle 'Chanterelle'; portobello 'Portobello'; chicken_of_the_woods 'Chicken of the Woods'; End-Enum; Dcl-s mushroom Char(20) Inz(‘False Morel); Select %Trim(mushroom); When-in edible_mushrooms; Dsply (%Trim(mushroom) + ': This mushroom is edible'); Other; Dsply (%Trim(mushroom) + ': This mushroom is NOT edible'); EndSl; Return; End-Proc enum_test;
Running this version results in the same output:
Learning this new functionality takes practice. I encourage you to download these examples, compile them, run them and debug them to see how they work!
Until next time, happy coding.
Gregory Simmons is a software engineer with P.C. Richard & Son. He started on the IBM i platform in 1994, graduated with a degree in Computer Information Systems in 1997 and has been working on the OS/400 and IBM i platform ever since. He has been a registered instructor with the IBM Academic Initiative since 2007, and holds a COMMON Application Developer certification. When he’s not trying to figure out how to speed up legacy programs, he enjoys speaking at technical conferences, running, backpacking, hunting, and fishing.
RELATED STORIES
Guru: RPG Select Operation Gets Some Sweet Upgrades
Guru: Growing A More Productive Team With Procedure Driven RPG
Guru: With Procedure Driven RPG, Be Precise With Options(*Exact)
Guru: Testing URLs With HTTP_GET_VERBOSE
Guru: Fooling Around With SQL And RPG
Guru: Procedure Driven RPG And Adopting The Pillars Of Object-Oriented Programming
Guru: Getting Started With The Code 4 i Extension Within VS Code
Guru: Procedure Driven RPG Means Keeping Your Variables Local
Guru: Procedure Driven RPG With Linear-Main Programs
Guru: Speeding Up RPG By Reducing I/O Operations, Part 2
Guru: Speeding Up RPG By Reducing I/O Operations, Part 1
Guru: Watch Out For This Pitfall When Working With Integer Columns
I don’t understand the last example. Isn’t Morel in the list of edible mushrooms?
By the way, your first example is missing a closing parenthesis: Main(enum_test
The last example is wrong – either the initialisation of mushroom or the output.
Thanks guys, I’ll submit an edit to get those corrected.