Wednesday, May 17, 2017

Defining LDA in free format RPG

how to define the lda in a free format rpg program

It has been pointed out that this web site lacks a good description of how to retrieve the data from the Local Data Area, LDA, in the post about defining variables in free format RPG. I agree, which is why I have written this about retrieving and updating the LDA.

I am going assume that we all know what the LDA is. If you do not then you can read IBM's definition of what it is.

There are two common ways to bring in and update the data from the LDA in fixed format RPG.

  1. Using the UDS data structure
  2. Using in and out to retrieve and update the data area

I need to describe how to replace both of those in free format RPG.

 

Replacing UDS data structure

In its simplest form the UDS data structure, as defined below, will bring the contents of the LDA into the data structure's subfields at program initialization, and update the LDA when the program ends.

    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++
01  D                UDS
02  D F1                      1      1A
03  D F2                      2      2A
04  D F3                      3      3A
05  D F4                      4      4A
06  D F5                      5      5A

The same functionality is available in free format definitions, but not using UDS.

01  dcl-ds *n dtaara(*auto) ;
02    F1 char(1) ;
03    F2 char(1) ;
04    F3 char(1) ;
05    F4 char(1) ;
06    F5 char(1) ;
07  end-ds ;

Line 1: The *N indicates that I have not given this data structure a name. If I give this data structure a name the compile will try to find a use a data area of that name rather than the LDA. The DTAARA(*AUTO) is the equivalent of the fixed format's UDS.

Let me add this data structure to a small program to demonstrate how the LDA is retrieved and updated without me needing to do anything to the data area in my RPG code.

01  dcl-ds *n dtaara(*auto) ;
02    F1 char(1) ;
03    F2 char(1) ;
04    F3 char(1) ;
05    F4 char(1) ;
06    F5 char(1) ;
07  end-ds ;

08  F1 = 'X' ;

09  *inlr = *on ;

This is the LDA before I run this program:

Data area . : *LDA
Type  . . . : *CHAR
Length  . . : 1024
Text  . . . : *LDA for Job 000000/SIMON/QPADEV0001

           Value
Offset      *...+....1....+....2
    0      '12345              '

Line 8: The value of data structure subfield F1 is changed to 'X'.

When I look at the LDA after the program has completed I can see that the first character has changed.

Data area . : *LDA
Type  . . . : *CHAR
Length  . . : 1024
Text  . . . : *LDA for Job 000000/SIMON/QPADEV0001

           Value
Offset      *...+....1....+....2
    0      'X2345              '

Rather than have to code all of the data structure's subfields I can use a file as an externally described data structure. This is a good idea as I can ensure that the same data structure is used in many programs, stopping me from mis-mapping a value in one program which was moved to the LDA by another.

  D               EUDS                  extname('LDAFILE')


  dcl-ds *n extname('LDAFILE') dtaara(*auto) ;
  end-ds ;

 

Using in and out to retrieve and update the LDA

Rather than have the program retrieve the values in the LDA at program initialization and update at the end of the program, I can decide to control when I retrieve and when I update. To do so I must define my data structure slightly differently. If I was doing this in fixed format it would look like this:

    DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++
01  D MyLDA           DS                  dtaara(*lda)
02  D  F1                     1      1A
03  D  F2                     2      2A
04  D  F3                     3      3A
05  D  F4                     4      4A
06  D  F5                     5      5A

But as I have shaken off the shackles of fixed format I would code it like this:

01  dcl-ds MyLda dtaara(*lda) qualified ;
02    F1 char(1) pos(1) ;
03    F2 char(1) pos(2) ;
04    F3 char(1) pos(3) ;
05    F4 char(1) pos(4) ;
06    F5 char(1) pos(5) ;
07  end-ds ;

Line 1: In both code snippets notice that the DTAARA keyword now contains *LDA, as you can well imagine this means that when I "in" MyLda it is the values from the LDA that are moved into the data structure. In the free format version I have used the QUALIFIED keyword so that I will need to qualify the subfields with the data structure's name (I could do the same with the fixed format too).

The rest of the program is really simple:

08  in MyLda ;
09  MyLda.F1 = 'X' ;
10  out MyLda ;

11  *inlr = *on ;

Line 8: I don't have to lock the LDA when I IN as I am the only person who can use it, no conflicts possible.

Line 9: As before I change the value of the first subfield.

Line 10: The OUT updates the LDA.

I am not going to bother to show the before and after of the LDA as the results are exactly the same as the previous example.

 

Now I have examples of how to use the LDA in free format programs.

 

You can learn more about this from the IBM website:

 

This article was written for IBM i 7.3, and will work for release 7.1 TR7 and later too.

1 comment:

  1. Always good to keep up with Poweri as it moves forward.

    ReplyDelete

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.