• 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

    December 7, 2020 Jon Paris

    In this time of pandemic we could all do with a little cheering up. So “Santa” Barbara (a.k.a. Barbara Morris) and the elves at the IBM Toronto Lab have delivered an early Christmas present. Available now via PTF for 7.3 and 7.4, there are some real gems in these latest RPG enhancements.

    For the most part, these enhancements assist in improving code readability. That is to say that they are not giving us completely new functionality in the way that (say) Open Access or DATA-INTO did. Rather they give us better, clearer ways of doing things. They have an additional benefit in that by implementing functionality that is common in other languages, they help make the transition to RPG easier for newcomers to the language.

    Let’s start with two of these enhancements, best demonstrated with an example. Suppose that you have an item-type code that must be “A”, “B”, “C” or “Z”. A conventional IF statement to check if the code is valid might look like this:

    IF ( typeCode = 'A' ) or ( typeCode = 'B' ) 
       or ( typeCode = 'C' ) or ( typeCode = 'Z' );
    

    Of course, the more options there are the worse the code gets. Compare that with using the new BIF %LIST together with the new IN operator. As you can see, it simplifies the coding significantly:

    IF typeCode IN %List( 'A' : 'B' : 'C' : 'Z' );
    

    Now isn’t that a lot cleaner and more obvious? Big, big improvement in my opinion. And even better if you want to test for a code NOT being in the list. A word of caution though. My first thought was to simply add the keyword NOT immediately after the IF, as in the first example below, but this does not work. Rather, you need to place the whole condition in parentheses and then negate the result with NOT as in the second example.

    // This version will not compile
    IF NOT typeCode IN %List( 'A' : 'B' : 'C' : 'Z' );
    // But this one does
    IF NOT ( typeCode IN %List( 'A' : 'B' : 'C' : 'Z' ) );
    

    It makes sense if you think about it but I thought I’d mention it just in case your initial reaction was the same as mine.

    There are other uses for both the IN operator and %LIST beyond this type of test, although frankly, I’d be happy enough if this was all they did. Let’s look at %LIST first.

    %LIST actually represents a temporary array and can therefore be used as such. So if, for example, I had a series of non-contiguous fields that I wanted to be able to load into an array, I could use %LIST to simplify the task, like this:

    fieldArray = %LIST( Field1 : Field2 : Field3 : Field4 : Field5 );
    

    This will result in the contents of Field1 being assigned to element 1, Field2 to the second element, and so on. I think you will agree that this is a lot cleaner and simpler than a series of assignments to the individual array elements.

    In fact the %LIST option is so flexible that it allows not just simple fields and constants, but expressions and procedure calls as well, as long as the base data type matches the array definition. For example, if the data type returned by GetNearestWarehouse() matches that of the warehouseList array, this code would work just fine.

    warehouseList = %List( preferredWarehouse
                         : GetNearestWarehouse(accountNumber)
                         : 'Chicago' );
    

    You might wonder, since %LIST represents an array, whether IN can also be used to search a conventional array. The answer of course is yes. Here’s a comparison between doing it the “old” way and using the IN option:

    If %Lookup( 'Two' : fieldArray ) > 0;
      Dsply 'I found it!';
    EndIf;
    
    If 'Two' IN fieldArray;
      Dsply 'I found it!';
    EndIf;
    

    In this case I searched for the literal “Two” but the search argument can be anything that is compatible with the data type of the array being searched. Once again, a little simpler and more obvious I think.

    Not Only %LISTs But Also %RANGEs

    The new IN option is not just limited to working with %LISTs. It can also be used to test if a value is in a specified range by using the %RANGE BIF. Here’s an example of it in use:

    If orderValue IN %Range( minimumOrder : customerMaximum );
    // Is equivalent to
    If ( orderValue >= minimumOrder ) 
       and ( orderValue <= customerMaximum );
    

    The range values can be literals, fields, named constants, etc. etc. Again they simply have to match the base type of the comparison value. Once again, it’s not something we couldn’t do before, but a big improvement nonetheless. Just in case you are wondering, %RANGE is inclusive. That is, it includes the from and to values. So for %Range( 3 : 6 ), the values 3, 4, 5, and 6 are all considered in the range.

     What Else Did Santa Bring?

    There are two other significant enhancements in this package. The first is the arrival of FOR-EACH loops in RPG, something I have been hoping for since encountering them in PHP. The second provides new compiler options to resolve issues related to character-to-numeric conversions. I will be covering both of these features in my next Tip.

    To see the full list of enhancements and the PTFs that are needed to implement them go the RPG Cafe page at https://ibm.biz/rpg_cafe.

    I hope you find these new features as useful in your coding as I know I will.

    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. He hosts the RPG & DB2 Summit twice per year with partners Susan Gantner and Paul Tuohy.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

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

    Sponsored by
    LaserVault

    Integrate Virtual Tape For Better Backups, Faster Recovery, And More Flexibility

    Virtual tape and virtual tape libraries offer a way to both simplify and strengthen backup and recovery operations. By incorporating virtual tape technology, automation of backups becomes possible resulting in hundreds of hours saved annually for IT departments and personnel.

    LaserVault ViTL is a virtual tape and tape library solution developed specifically for use with IBM Power Systems (from AS/400 to iSeries to Power 9s). See a demo and get a $50 gift card.

    With ViTL you can:

    • Replace physical tape and tape libraries and associated delays
    • Automate backup operations, including the ability to purge or archive backups
    • Remotely manage your backups – no need to be onsite with your server
    • Save backups to a dedupe appliance and the cloud
    • Recover your data at lightspeed greatly improving your ability to recover from cyberattacks
    • And so much more

    “The ViTL tapeless solution has truly made my job easier. It has given me more confidence in our full system recovery ability – but at the same time I hope it is never needed.” IBM i Administrator at a financial services company

    Sign-up now to see a ViTL online demo and get a $50 Amazon e-gift card when the demo is complete as our way of saying thanks for your time. Plus when you sign-up you’ll receive a free facts comparison sheet on using virtual tape vs tape so you can compare the functionality for yourself.

    LaserVault.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: Disruption Revisited Thoroughly Modern: What’s New With PHP On IBM i?

    2 thoughts on “Guru: Fall Brings New RPG Features”

    • Ghost says:
      December 7, 2020 at 8:09 am

      Brilliant Jon! Santa has absolutely delivered.
      Can’t wait to get my hands on these new bifs.

      Thanks for the write up and especially for the “not in list” example – I would’ve done exactly what you did.

      Reply
    • Tim Molter says:
      December 8, 2020 at 9:11 am

      This seems to make more sense to me coming from an SQL background: IF typeCode NOT IN %List( ‘A’ : ‘B’ : ‘C’ : ‘Z’ );

      Reply

    Leave a Reply Cancel reply

TFH Volume: 30 Issue: 78

This Issue Sponsored By

  • UCG Technologies
  • Fresche Solutions
  • ARCAD Software
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • To 2032 . . . And Beyond!
  • Thoroughly Modern: What’s New With PHP On IBM i?
  • Guru: Fall Brings New RPG Features
  • As I See It: Disruption Revisited
  • MariaDB Now Available Via RPM

Content archive

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

Recent Posts

  • COMMON Set for First Annual Conference in Three Years
  • API Operations Management for Safe, Powerful, and High Performance APIs
  • What’s New in IBM i Services and Networking
  • Four Hundred Monitor, May 18
  • IBM i PTF Guide, Volume 24, Number 20
  • IBM i 7.3 TR12: The Non-TR Tech Refresh
  • IBM i Integration Elevates Operational Query and Analytics
  • Simplified IBM i Stack Bundling Ahead Of Subscription Pricing
  • More Price Hikes From IBM, Now For High End Storage
  • Big Blue Readies Power10 And IBM i 7.5 Training for Partners

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 © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.