How To
Summary
An RPG programmer wants to substitute the values of variables ITEM and PRICE in the following string
"The price of item %ITEM% is $%PRICE%.".
The programmer can use DATA-GEN to handle this.
Objective
The program that uses the DATA-GEN opcode
The source is also provided as an attachment to this article.
**free
// price_msg - a data structure for the MULTRPL generator for DATA-GEN
// - the first subfield is the message template
// - the second subfield is the character that surrounds the replacement names
// - the remaining subfields are the replacement subfields
dcl-ds price_msg qualified;
message varchar(100) inz('The price of item "%ITEM%" is $%PRICE%.');
marker char(1) inz('%');
item varchar(20) inz('Table');
price packed(5:2) inz(123.45);
end-ds price_msg;
// A variable to receive the formatted message
dcl-s output char(50);
// This DATA-GEN will substitute ITEM and PRICE
data-gen ds %data(output) %gen('MULTRPL');
// Display the result: The price of item "Table" is $123.45.
dsply output;
return;
Steps
The DATA-GEN generator program MULTRPL.
The source is also provided as an attachment to this article.
**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"
// message inz('The price of item "%ITEM%" is $%PRICE%')
// - the second subfield is the character placed before
// and after the replacement values
// marker inz('%')
// - the remaining subfields are the replacement values
// - they can have any data type
// item varchar(20) inz('Table');
// price packed(7:2) inz(123.45);
// 2. Specify the data structure as the first operand of DATA-GEN
// 3. Specify this program for %GEN
/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;
Document Location
Worldwide
Was this topic helpful?
Document Information
More support for:
Rational Development Studio for i
Component:
IBM i
Software version:
All Versions
Document number:
6508862
Modified date:
26 October 2021
UID
ibm16508862