• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • 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

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, IBM i 7.4 TR9, IBM i 7.5 TR3, RPG

    Sponsored by
    New Generation Software

    Put your IBM i Data to Work

    NGS-IQ makes it easy to securely access, format, analyze, and share critical business data across your organization.

    Join us for a look at the top challenges companies attending COMMON NAViGATE at TechXchange 2026 shared when it comes to getting reliable reports from their DB2 data — and how NGS-IQ can help provide the answers you need without breaking the budget.

    Register for the Webinar

    www.ngsi.com – (800)824-1220

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Gartner Raises IT Spending Forecast For 2024 Again Power Systems Distributor Ingram Micro Goes Public (Again)

    3 thoughts on “Guru: RPG Receives Enumerator Operator”

    • Glenn Gundermann says:
      November 11, 2024 at 9:31 am

      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

      Reply
    • Kevin Wright says:
      November 11, 2024 at 3:46 pm

      The last example is wrong – either the initialisation of mushroom or the output.

      Reply
    • Gregory Simmons says:
      November 14, 2024 at 7:33 am

      Thanks guys, I’ll submit an edit to get those corrected.

      Reply

    Leave a ReplyCancel reply

TFH Volume: 34 Issue: 55

This Issue Sponsored By

  • Fresche Solutions
  • Meridian IT
  • WorksRight Software
  • ARCAD Software
  • Raz-Lee Security

Table of Contents

  • Fresche Nabs Redbourn Business Systems For Synon Expertise
  • Power Systems Distributor Ingram Micro Goes Public (Again)
  • Guru: RPG Receives Enumerator Operator
  • Gartner Raises IT Spending Forecast For 2024 Again
  • IBM i PTF Guide, Volume 26, Number 44

Content archive

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

Recent Posts

  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38
  • Critical PHP2Shell Security Vulns Evoke ‘Black October’
  • Why Fortra Partnered with FalconStor to Launch Continuity Care: A Validated Recovery Service for IBM i
  • Guru: Analyzing User Session Statistics, Part 2
  • Fresche Launches OpsIQ, a Free AI-Based Operation Assistant for IBM i
  • IBM i PTF Guide, Volume 28, Number 37

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