IBM Support

RPG Cafe: Fall 2020: New BIFs, FOREACH opcode

News


Abstract

RPG enhancement with new built-in functions %RANGE and %LIST, and new opcode FOR-EACH.

Content

You are in: RPG Cafe > RPG Cafe: New BIFs, FOR-EACH opcode

Short URL: https://ibm.biz/rpgcafe_fall_2020_new_bifs_foreach_opcode

RPG Cafe: New BIFs, FOR-EACH opcode

New opcode FOR-EACH, and new built-in functions %RANGE and %LIST

  • %RANGE: You can check whether a value is in the range of two other values by using the %RANGE built-in function.
  • %LIST: You can create a temporary array by using the %LIST built-in function.
  • 'IN' operator: You can check whether a value is "in" a range of values or in an array or %LIST.
  • FOR-EACH opcode: You can iterate over an array or %LIST using the FOR-EACH opcodes. The FOR-EACH opcode begins a structured group that ends with the ENDFOR opcode.

Details

%RANGE

In this example, the IF statement is checking whether the current date is between the minimum and maximum registration dates.

Using x IN %RANGE(y : z) is the same as x >= y AND x <= z.

          IF %DATE() IN %RANGE(min_register_date : max_register_date);
             processRegistration (); // The date is in range
          IF;

%LIST

In this example, %LIST is used to create a temporary array to assign values to the names array.

          DCL-S names VARCHAR(20) DIM(3);

          names = %LIST('Mary' : 'Jack' : 'Alice');

IN operator

In this example, the 'IN' operator is used to check whether cust_city is equal to one of the values in the sale_cities array. %SUBARR is used to limit the number of elements that are checked.

Then, the 'IN' operator is used to check whether the cust_status is one of the values in the %LIST array.

          DCL-S sale_cities VARCHAR(20) DIM(10);
          DCL-S num_sale_cities INT(10);

          IF city IN %SUBARR(sale_cities : 1 : num_sale_cities);
             IF cust_status IN %LIST(STATUS_PREFERRED : STATUS_GOLD);
                subtract_discount (); // This customer gets a discount

FOR-EACH opcode

In this example, the FOR-EACH opcode is used to iterate over the values in the %LIST array.

Then, the FOR-EACH opcode is used to iterate over the elements in the errors array. %SUBARR is used to limit the number of elements that are processed.

          DCL-DS error LIKEDS(error_t);
          DCL-DS errors LIKEDS(error_t) DIM(100);
          DCL-S num_errors INT(10);
          DCL-S error_type INT(10);
          DCL-S filename VARCHAR(21);
          DCL-F logfile EXTDESC('LOGFILE_T') EXTNAME(filename);

          FOR-EACH filename IN %LIST('CUR_ERRORS' : 'ALL_ERRORS');
             OPEN logfile;
             FOR-EACH error IN %SUBARR(errors : 1 : num_errors);
                log_error_message (error);
             ENDFOR;
          ENDFOR;
.                                                            .
.                                                            .

PTFs for 7.3 and 7.4, available in November 2020

7.3:

  • ILE RPG compiler: SI74612

7.4:

  • ILE RPG compiler: SI74613
  • ILE RPG compiler, TGTRLS(V7R3M0): SI74614

The PTFs will also be available with the Db2 for i Fix Packs. See Db2 for IBM i 2020 PTF Group Schedule.

RDi support

The next release of RDi will support this enhancement.

Documentation in the IBM Knowledge Center

The ILE RPG Reference and ILE RPG Programmer's Guide in the 7.3 and 7.4 IBM Knowledge Center are updated with full information about this enhancement. Start at the What's New Since 7.3 or What's New Since 7.4 section in the Reference.

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SS69QP","label":"Rational Development Studio for i"},"Component":"ILE RPG Compiler","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"7.3;7.4","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]

Document Information

Modified date:
12 November 2020

UID

ibm16342821