• 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:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, IBM i 7.4 TR9, IBM i 7.5 TR3, 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

    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 Reply Cancel 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

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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