IBM Support

RPG Cafe: How to write a procedure to replace the values in a string template using OPTIONS(*CONVERT)

How To


Summary

How to write a procedure that your callers can easily call to replace the values in a string template with other variables or expressions in their program.

Objective

Write the "format" procedure that supports the following calls, where the programmer who codes the call does not have to worry about the data type of the replacement parameters.
string = format ('My name is [REP1] and my age is [REP2]. I am [REP2] years old.'
               : name
               : %diff (%date() : birthDate : *years));

Environment

Using ILE RPG, at a release and PTF level that supports OPTIONS(*CONVERT). See OPTIONS(*CONVERT) enhancement for PTF details.

Steps

Step 1. Create the prototype and place it in a copy file.

In later steps in this example, the copy file is assumed to be either member FORMAT_H in source file *LIBL/QRPGLESRC or file format_h.rpgleinc in an IFS directory available for the compile.

This code is also in attachment format_h.rpgleinc.txt.

           dcl-pr format varucs2(2000) extproc(*dclcase);
              template varucs2(2000) const;
              rep1 varucs2(200) const options(*nopass : *convert);
              rep2 varucs2(200) const options(*nopass : *convert);
              rep3 varucs2(200) const options(*nopass : *convert);
              rep4 varucs2(200) const options(*nopass : *convert);
              rep5 varucs2(200) const options(*nopass : *convert);
              rep6 varucs2(200) const options(*nopass : *convert);
              rep7 varucs2(200) const options(*nopass : *convert);
              rep8 varucs2(200) const options(*nopass : *convert);
              rep9 varucs2(200) const options(*nopass : *convert);
           end-pr;

Step 2. Write the procedure, copying in the prototype from the copy file

This code is also in attachment format_proc.rpgle_.txt.


           /copy format_h
           dcl-proc format export;

              dcl-pi *n varucs2(2000) ;
                 template varucs2(2000) const;
                 rep1 varucs2(200) const options(*nopass : *convert);
                 rep2 varucs2(200) const options(*nopass : *convert);
                 rep3 varucs2(200) const options(*nopass : *convert);
                 rep4 varucs2(200) const options(*nopass : *convert);
                 rep5 varucs2(200) const options(*nopass : *convert);
                 rep6 varucs2(200) const options(*nopass : *convert);
                 rep7 varucs2(200) const options(*nopass : *convert);
                 rep8 varucs2(200) const options(*nopass : *convert);
                 rep9 varucs2(200) const options(*nopass : *convert);
              end-pi;

              dcl-s result varucs2(2000);

              result = template;
              if %parms() >= %parmnum(rep1);
                 result = %scanrpl(%ucs2('[REP1]') : rep1 : result);
              endif;
              if %parms() >= %parmnum(rep2);
                 result = %scanrpl(%ucs2('[REP2]') : rep2 : result);
              endif;
              if %parms() >= %parmnum(rep3);
                 result = %scanrpl(%ucs2('[REP3]') : rep3 : result);
              endif;
              if %parms() >= %parmnum(rep4);
                 result = %scanrpl(%ucs2('[REP4]') : rep4 : result);
              endif;
              if %parms() >= %parmnum(rep5);
                 result = %scanrpl(%ucs2('[REP5]') : rep5 : result);
              endif;
              if %parms() >= %parmnum(rep6);
                 result = %scanrpl(%ucs2('[REP1]') : rep1 : result);
              endif;
              if %parms() >= %parmnum(rep6);
                 result = %scanrpl(%ucs2('[REP6]') : rep6 : result);
              endif;
              if %parms() >= %parmnum(rep7);
                 result = %scanrpl(%ucs2('[REP7]') : rep7 : result);
              endif;
              if %parms() >= %parmnum(rep8);
                 result = %scanrpl(%ucs2('[REP8]') : rep8 : result);
              endif;
              if %parms() >= %parmnum(rep9);
                 result = %scanrpl(%ucs2('[REP9]') : rep9 : result);
              endif;

              return result;
           end-proc format;

Step 3. Test the procedure

This code is also in attachment format_test.rpgle_.txt.

           /copy format_h

           dcl-s string char(50);
           string = format('No parms');
           dsply string;
           string = format('All parms, rep 1,2,9 [REP1] [REP2] [REP9]'
                         : 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9);
           dsply string;
           string = format('2 parms, rep parm 2 twice [REP2] [REP1] [REP2]'
                         : 1 : 'two');
           dsply string;
           return;

Document Location

Worldwide

[{"Type":"MASTER","Line of Business":{"code":"LOB57","label":"Power"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SS69QP","label":"Rational Development Studio for i"},"ARM Category":[{"code":"a8m0z0000000CHtAAM","label":"Programming ILE Languages"}],"ARM Case Number":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"7.4.0;7.5.0;and future releases"}]

Document Information

Modified date:
11 April 2023

UID

ibm16982933