Thursday, May 28, 2020

Define a new Data Structure from one that is nested

using nested data structures in likeds

In the past I have written about nesting data structures, a way to contain data structure(s) within another data structure. And there are times I have wanted to define a new data structure to be identical to another using the LIKEDS keyword.

01  dcl-ds One qualified ;
02    OneSF1 char(1) ;
03    dcl-ds Two ;
04      TwoSF char(5) ;
05    end-ds ;
06    OneSF2 char(1) ;
07  end-ds ;

08  dcl-ds Test1DS likeds(One) ;

Lines 1 – 7: I have one data structure, One.

Lines 3 – 5: I have another data structure embedded within it, Two.

Line 8: I have defined this new data structure using the qualified name of the data structure One, and this one will also include Two.

After compiling the program I can look in the source listing and see that Test1DS is the same size as One.

Field       Attributes
ONE         DS(7)
  ONESF1    A(1)
  ONESF2    A(1)
  TWO       DS(5)
    TWOSF   A(5)

TEST1DS     DS(7)

I can even use the nested data structure, Two, in my new data structure:

10  Test1DS.OneSF2 = 'Y' ;
11  Test1DS.Two.TwoSF = '12345' ;

Debug: EVAL test1ds
TEST1DS.TWO.TWOSF = '12345' 

What if I want to define a data structure like the nested data structure?

09  dcl-ds Test2Ds likeds(One.Two) ;

Previously when I compile the program with line 9 in it I would get the following errors:

09 dcl-ds Test2DS likeds(One.Two) ;
===>                        a
===>                     bbbbbbb
*RNF0622 20 a   000800  A qualified name is not allowed in this 
*RNF3371 20 b   000800  The parameter ONE.TWO for keyword LIKEDS
                           is not a data structure.

Fortunately the new Technology Refreshes, IBM i 7.4 TR2 and 7.3 TR8, "fixed" this. When I compiled the program, on a partition with the new TRs, no error occurred and I can see the new data structure is defined as I expected:

Field       Attributes
ONE         DS(7)
  ONESF1    A(1)
  ONESF2    A(1)
  TWO       DS(5)
    TWOSF   A(5)

TEST2DS     DS(5)

Line 12, below, shows I can use the qualified subfield when using the new data structure.

12  Test2DS.TwoSF = '54321' ;

Debug: EVAL test2ds
TEST2DS.TWOSF = '54321'


You can learn more about the changes to RPG's LIKEDS from the IBM website here.


This article was written for IBM i 7.4 TR2 and 7.3 TR8.

No comments:

Post a Comment

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.