Pages

Wednesday, June 28, 2023

Changes to the RPG Select operation code

In my RPG work I have been using the IF-ELSEIF operations code instead of Select groups for longer than I have been writing this web site. I preferred them as I could do the same as a Select operation code in less lines of code.

But then comes along two new RPG operation codes in the latest Technology Refreshes, IBM i 7.5 TR2 and 7.4 TR8, that are related to the Select operation code that might change my mind.

The two new RPG operation codes are:

  • WHEN-IS:  Used to compare the given value is equal to a value or variable
  • WHEN-IN:  Used to compare the given value is in a list or values, array, or in a range

The two can be in the same Select group.

Let me start with an example of how I had to use the Select group:

01  select ;
02    when %upper(Unit) = 'IN' ;
03      Description = 'INCHES' ;
04    when %upper(Unit) = 'FT' ;
05      Description = 'FEET' ;
06    when %upper(Unit) = 'YD' ;
07      Description = 'YARD' ;
08    when %upper(Unit) = 'MI' ;
09      Description = 'MILES' ;
10  endsl ;

I have to convert the contents of the variable I am using to compare, Unit, to upper case, with the %UPPER built in function, on every line.

Below is what the same would look like using the new WHEN-IS op-code:

01  select %upper(Unit) ;
02    when-is 'IN' ;
03      Description = 'INCHES' ;
04    when-is 'FT' ;
05      Description = 'FEET' ;
06    when-is 'YD' ;
07      Description = 'YARD' ;
08    when-is 'MI' ;
09      Description = 'MILES' ;
10  endsl ;

Line 1: I only need to give the variable whose value I will be checking next to the SELECT.

The WHEN-IS allows me to just give the comparison value, or variable, That is compared to the variable on the Select line. IMHO this is easier to understand than using the WHEN op-code.

The WHEN-IN op-code is even more intriguing than the WHEN-IS. Below is my example Select group using the WHEN-IN:

01  select Color ;
02    when-in %list('ORANGE':'PINK':'CYAN':'BROWN') ;
03      Code = '1' ;
04    when-in Colors ;
05      Code = '2' ;
06    when-in %range('AARDVARK' : 'AXE') ;
07      Code = '3' ;
08    when-is 'AQUA' ;
09      Code = '4' ;
10  endsl ;

Line 1: Again the variable I will be testing is placed next to the SELECT.

Line 2: I am testing the value in the variable to the list of the values offered by the %LIST built in function, BiF.

Line 4: This is where it is really cool! The WHEN-IN performs the equivalent of a %LOOKUP BiF, or LOOKUP op-code, to check if the value in the variable Color is found in the array Colors.

Line 6: Another allowed comparison is using the %RANGE BiF. If the value is between "AARDVARK" and "AXE" then this is regard as successful.

Line 8: This is just to show that I can intermingle the two new op-codes, as this is a WHEN-IS op-code amongst the WHEN-IN.

I can certainly see myself using both WHEN-IN and WHEN-IS in my RPG code.

 

You can learn more about this from the IBM website:

 

This article was written for IBM i 7.5 TR2 and 7.4 TR8.

9 comments:

  1. Sweet!! Thanks Simon!

    ReplyDelete
  2. Great! Thanks Simon for being an invaluable source of information!

    ReplyDelete
  3. Thanks you for your share, Simon!

    ReplyDelete
  4. thanks Simon! I too may have to reconsider using Select again.

    ReplyDelete
  5. I never used If-Else. I do a lot of maintenance programming and find in my opinion that Select is much easier and faster to read than If-Else.

    ReplyDelete
  6. All this cool stuff after I retired.... Dang!

    ReplyDelete
  7. And you can spare yourself temporary variables, when using SELECT with the result of a procedure.

    select getMyResult(xyz);
    when-is ...;
    when-is ...;
    other;
    endsl;

    You can't do that with if/elseif/else/end without using a variable for the procedure result.

    ReplyDelete
  8. It would be nice to have a when-like, or maybe when-is %like()

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.