Tuesday, September 10, 2013

Renaming one field in a file in RPG

I was asked to quickly modify a program. The program currently uses a file, I'll call it FILE1, and the modification was to add a second file, FILE2 to it. If the user selects to search for a certain type of information the the program would uses FILE1, if they want to search for a different type of information the program would need to use FILE2.

All of the fields in FILE1 and FILE2 have the same names and attributes, except for the Part number field. The Part number field, PART, in FILE1 is 20 long, in FILE2 it is 15.

I cannot change FILE2 to make PART be the same length as it is in the other file. I must make the minimum amount of changes to the program to negate the need for it to be audited by the SOX auditors. What can I do?

The changes I would need to make would be:

  • Create a 'which file' field to be used to determine which file the search is for.
  • Add USROPN to the File specifications, F-spec, for FILE1 and FILE2. This means that the file will only be opened if I use the OPEN operation code to open it.
  • Use the value in the 'which file' field to condition which file is opened.
  • Decide which file to read by using the 'which file' field.
  • Close the file opened.

But that still leaves the problem of what to do with the two PART fields.

But how do I cope with the difference between the two PART fields with the minimum of code changes?

Using the PREFIX keyword in the F-spec would not do, as that would rename all the fields in FILE2.

Then it struck me, I could use a way I had used before in RPG III.

Years ago I worked with a company with files that had been developed by Cobol programmers. Many of the field names were longer than six characters, which is the maximum length a field name can be in RPG III. Therefore, they had to be renamed in the RPG programs before they could be used.

What I did was to rename the "external description" of the fields in the Input specification, I-spec. I can do the same here.

01 IFILE2R
02 I              PART                        PART_2
    /free
03     read FILE2 ;
04     if not(%eof) ;
05       PART = PART_2 ;
06     endif ;

Line 1 has the record format name.

The first field name on line 2 is the file name from the file, the second field name is what it will be called in this program.

Now when FILE2 is read, line 3, all I have to do is move the value in PART_2 to PART, line 5, and I don't have to make any more changes.

Do you know of a better way of doing this? If so post it in the Comments below.

To learn more about renaming fields in the I-spec see here.

 

This article was written for IBM i 7.1, and it should work with earlier releases too.

2 comments:

  1. Hi Simon,

    You may also do the same in D-specs with the EXTFLD keyword.

    ReplyDelete
    Replies
    1. Thank you for mentioning the EXTFLD. I have create a post about it and how it compares to this method. The post will be published next week (Nov 7, 2013).

      Delete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.