Let’s talk about one of the many advantages of free-format coding.
If you’re reading this article you’re likely an RPG IV programmer. Have you been using free-format calculations for a while? My guess is that most of you are. Have you been using the new free-format H-, F-, D-, and P-specs? My guess is not so much. Change is hard, and from my experience you have to give something new a little time, then quit, then give it a little time again—later on. My goal in writing this article for you is to give you a little bit of this new information on a subject that is fairly common.
With IBM i release 7.2 and forward, the use of free-format control statements, file statements, definition statements, and procedure definition statements is now possible without PTFs or technical refreshes. This article focuses on the free-format definitions and specifically coding for the local data area (LDA) and a data area object in a data structure.
Local Data Area (LDA)
To define the local data area in a data structure in free-format, there are two methods available, just as there was in fixed-format. The first method automatically reads the information from the LDA into the RPG data structure during the program load initialization phase, and then it writes the data structure data back to the LDA at program close.
In fixed-format, this was done with the following program statement:
D UDS
D Fld1 1 10
C EVAL Fld1 = ‘New Value ’
The equivalent code in free-format would be as follows:
Dcl-Ds *n DtaAra(*Auto);
Fld1 Char(10);
End-Ds;
Fld1 = ‘New Value ‘;
Again, the field Fld1 is modified during RPG’s initialization phase to be the first 10 bytes of the LDA. When Fld1 is modified during the program procedure, the new value of Fld1 is written to positions 1 to 10 of the LDA at program end.
The second method does not automatically load the data structure. The op-codes of IN and OUT must be used.
In fixed-format, this was done with the following program statements:
D MYDS UDS
D Fld2 1 10
C In MyDS
C Eval Fld2 = ‘New Value ’
C Out MyDS
The equivalent code in free-format would be as follows:
Dcl-Ds MyDs DtaAra(*LDA);
Fld2 Char(10);
End-Ds;
In MyDs;
Fld2 = ‘New Value ’;
Out MyDs;
The LDA is commonly used in RPG IV programming, and converting the fixed-format definition to free-format may seem a little odd at first, but after confirming that it “works,” you will begin using it every day.
Data Area Object
Now, let’s look at the free-format method for defining a data area data structure for a data area object, not the LDA.
You can define a data area object in a data structure, with two methods available. As with the LDA, you can have the data structure automatically loaded (read) at program initialization time, and the contents of the data structure will be written to the data area object at program end. In this method, care must be exercised with regard to object locking. The data area object must not already be locked at program start, or you will get an error message. If the lock is obtained OK, then the data area object (to be updated) is locked from other users until this program ends.
The second method requires the use of the IN and OUT op-codes to read from the data area or write to the data area.
The following examples uses a 12-byte data area named JIMDTA. The following fixed-format coding method was used to define and use the first method—the one-time automatic read, and the end-of-program automatic write:
d Data12 uds
d TwelveB 1 12
d Field12 s 12
c *dtaara define JimDta Data12
c Eval Field12 = TwelveB
c Eval TwelveB = ‘New Value’
c Eval *Inlr = *On
Here is the equivalent coding using the free-format method:
Dcl-ds JimDta dtaAra(*auto);
TwelveB char(12);
End-ds;
Dcl-s Field12 Char(12);
Field12 = TwelveB;
TwelveB = 'New Value';
*Inlr = *on;
The second method does not automatically load the data structure. The op-codes of IN and OUT must be used. The following are examples of both the fixed-format method and the free-format equivalent:
d Data12 uds dtaara(JimDta)
d TwelveB 1 12
d Field12 s 12
c In Data12
c Eval Field12 = TwelveB
c Eval TwelveB = 'New Value'
c Out Data12
c Eval *Inlr = *on
Here is the equivalent code in free-format. Notice that the *Lock parameter must be used on the IN op-code in order to update the data area. The fixed-format example above used the “u” in the Data Structure Type column, which automatically locked the data area.
Dcl-Ds MyDs DtaAra(‘JIMDTA’);
TwelveB char(12);
End-Ds;
Dcl-s Field12 Char(12);
In *Lock MyDs;
Field12 = TwelveB;
TwelveB = ‘New Value’;
Out MyDs;
*Inlr = *On;
Data area objects are commonly used in RPG IV programming, and converting the fixed-format definition to free-format is fairly easy.
Going Free-Format
Hopefully, you are now more comfortable in using the free-format method for coding your RPG IV data area data structures. Now, go try something else!
LATEST COMMENTS
MC Press Online