URL: https://www.ibm.com/support/pages/system/files/inline-files/multrpl.rpgle__1.txt
**free
// A generator for the DATA-GEN opcode to substitute
// several values into a string
//
// Usage:
// 1. Define a data structure where
// - the first subfield is the string "template"
// - the second subfield is the character placed before
// and after the replacement values
// - the remaining subfields are the replacement values
// - they can have any data type
// dcl-ds price_msg qualified;
// message inz('The price of item "%ITEM%" is $%PRICE%')
// marker inz('%')
// item varchar(20) inz('Table');
// price packed(7:2) inz(123.45);
// end-ds;
// 2. Specify the data structure as the first operand of DATA-GEN and
// specify this program for %GEN
// DATA-GEN price_msg %DATA(outputString) %GEN('THISPGM');
/if defined(*crtbndrpg)
ctl-opt dftactgrp(*no);
/endif
ctl-opt option(*srcstmt);
ctl-opt debug(*constants);
ctl-opt ccsid(*ucs2 : *utf16);
ctl-opt main(multipleReplacements);
/copy qoar/qrpglesrc,qrndtagen
dcl-proc multipleReplacements;
dcl-pi *n;
parm likeds(QrnDgParm_T);
end-pi;
dcl-s subf varchar(100);
dcl-s i int(10);
dcl-s value ucs2(1000) based(pValue);
dcl-s subfValue varucs2(1000);
dcl-s replaceName varucs2(1000);
dcl-ds state qualified based(pState);
result varucs2(10000);
marker varucs2(10);
end-ds;
pQrnDgEnv = parm.env;
if parm.event = QrnDgEvent_03_Start;
// Start of generation: Allocate the "state" info
parm.generatorState = %alloc(%size(state));
endif;
pState = parm.generatorState;
if parm.event = QrnDgEvent_04_End;
// End of generation: Report the resulting string
QrnDgAddText (parm.handle
: %addr(state.result : *data)
: %len(state.result));
// Free the state info
dealloc(n) parm.generatorState;
elseif parm.event = QrnDgEvent_11_ScalarValue;
if parm.scalar.subfieldNumber = 1;
// The first subfield has the template string
pValue = parm.scalar.value;
state.result =
%subst(value : 1 : parm.scalar.valueLenChars);
elseif parm.scalar.subfieldNumber = 2;
// The second subfield has the substitution marker
// - it is placed before and after each substitution
// value in the template string
pValue = parm.scalar.value;
state.marker =
%subst(value : 1 : parm.scalar.valueLenChars);
else;
// The result of the subfields have the replacement
// values
pValue = parm.scalar.value;
subfValue = %subst(value : 1 : parm.scalar.valueLenChars);
replaceName = state.marker + %upper(parm.name) + state.marker;
state.result = %scanrpl(replaceName
: subfValue
: state.result);
endif;
endif;
return;
end-proc;