Load same data into multiple fields in one line of code

Discussions relating to writing software in ILE RPG (RPG IV). This includes both fixed and free format RPG.
Post Reply
heerschap
Posts: 2
Joined: Thu Nov 03, 2022 2:23 pm

Load same data into multiple fields in one line of code

Post by heerschap »

Hello and thank you in advance for the help

Program has multiple files

Code: Select all

F FileA uf a e k disk prefix(a_)
F FileB uf a e k disk prefix(b_)
F FileC uf a e k disk prefix(c_)
F FileD uf a e k disk prefix(d_)
F FileE uf a e k disk prefix(e_)

Several fields in these files have the same field name

Code: Select all

FieldName 15 TEXT('Field Name')
I wish to populate FieldName in all the files at once

Currently it's obviously like this

Code: Select all

/free
a_FieldName = data;
b_FieldName = data;
c_FieldName = data;
d_FieldName = data;
e_FieldName = data;
/end-free
jonboy49
Posts: 98
Joined: Wed Jul 28, 2021 8:18 pm

Re: Load same data into multiple fields in one line of code

Post by jonboy49 »

Well the simple answer would be to stop using prefix on the file definition. That way all those fields on all files would share the same single piece of storage and an update to that value followed by an update to all of the files would do the job. Whether that would work or not depends on why prefix is being used in the first place and what else is going on in the program.
heerschap
Posts: 2
Joined: Thu Nov 03, 2022 2:23 pm

Re: Load same data into multiple fields in one line of code

Post by heerschap »

thank you jonboy49
I understand what you are saying, but the input file can also have a field called FieldName.

I guess this would make more sense showing the code

Code: Select all

/free
a_FieldName = FieldName;
b_FieldName = FieldName;
c_FieldName = FieldName;
d_FieldName = FieldName;
e_FieldName = FieldName;
/end-free
But say no prefix and each field is named differently in each file
inputFile has field customer
outputfileA has field acustomer
outputfileB has field bcustomer
outputfileC has field ccustomer
outputfileD has field dcustomer
outputfileE has field ecustomer

It is possible to populate all 5 acustomer, bcustomer, ccustomer, dcustomer, ecustomer with customer without doing 5 separate lines of code?
jonboy49
Posts: 98
Joined: Wed Jul 28, 2021 8:18 pm

Re: Load same data into multiple fields in one line of code

Post by jonboy49 »

If you check the definition for prefix() you'll see that there is an option to specify a number of characters to strip off the existing name before adding the prefix. So you could achieve the renaming by saying Prefix( '' : 1 ) which would result in all the fields being named customer. Needless to say it will also rename all the other fields in the record and I have no idea whether that will affect your logic.
Post Reply