ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Overlay fields for array.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Overlay fields for array.

    I am not sure what I am doing wrong. It must be something silly that I am missing. Please see the examples below.

    Code:
         d                 ds
         dmyids                          10  0 dim(5)
         d id1                     1     10  0
         d id2                    11     20  0
         d id3                    21     30  0
         d id4                    31     40  0
         d id5                    41     50  0
    
         d                 ds
         d myids                         10  0 dim(5)
         d id1                           10  0 overlay(myids)
         d id2                           10  0 overlay(myids:*next)
         d id3                           10  0 overlay(myids:*next)
         d id4                           10  0 overlay(myids:*next)
         d id5                           10  0 overlay(myids:*next)
    
             dcl-ds *n;
                myids zoned(10:0) dim(5);
                id1 zoned(10:0) overlay(myids);
                id2 zoned(10:0) overlay(myids);
                id3 zoned(10:0) overlay(myids);
                id4 zoned(10:0) overlay(myids);
                id5 zoned(10:0) overlay(myids);
             End-Ds;
    So imagine that ID1..ID5 are fields in a file(such as below). Here I am trying to put them in an array when the record is read. If I use the first definition this works perfectly. However when I use the second and third definitions I get the error message "RNF4138: Externally-Described Field used as array name. Definition specification is ignored.", and the programs doesn't compile. Once in a while I have a need to do it this way...

    Code:
    A          R RTEST41P           
    A            ID1           10  0
    A            NAME1         20   
    A            ID2           10  0
    A            NAME2         20   
    A            ID3           10  0
    A            NAME3         20   
    A            ID4           10  0
    A            NAME4         20   
    A            ID5           10  0
    A            NAME5         20

  • #2
    Re: Overlay fields for array.

    Rather than defining the array first and overlaying the fields, try reversing it, like so:
    Code:
         d ids             ds
         d  id1
         d  id2
         d  id3
         d  id4
         d  id5
         d  myids                              dim(5) like(id1) overlay(ids)
    I took out the explicit length and decimal positions since it will get them from the external description. As to why the second example does not compile, my guess is that it does not take into consideration the DIM keyword when it does the overlays, so the first overlay works, but it doesn't know what to do with the second.

    Comment


    • #3
      Re: Overlay fields for array.

      Yeah this is still pretty much the same as my first definition that works great. I just don't understand why the second and third definitions don't work.

      Comment


      • #4
        Re: Overlay fields for array.

        As I said in my earlier post, the DIM keyword is probably not being taken into account when the array is being overlayed. The first example works because the from and to positions are explicitly given.

        Comment


        • #5
          Re: Overlay fields for array.

          That is a good point. I will give it a try and we will see.

          Comment


          • #6
            Re: Overlay fields for array.

            Mkay... So here is how you do it in free format.
            Code:
                     dcl-ds ids;
                        id1;
                        id2;
                        id3;
                        id4;
                        id5;
                        myids zoned(10:0) dim(5) [COLOR="#FF0000"]pos(1);[/COLOR]
                     End-Ds;
            The POS keyword is different, but it works.

            Comment


            • #7
              Re: Overlay fields for array.

              The reason it doesn't work when you code the array first is that *NEXT doesn't mean "after the previous subfield", it means "after all previous subfields".

              OVERLAY(myids:*NEXT) means the same thing as not coding the OVERLAY keyword at all. OVERLAY(*NEXT) is only meaningful for overlaying subfields.

              Edit: Sorry, I didn't read the example correctly. I thought "myids" was the data structure. But I'll leave in what I said about *NEXT anyway ... :-)
              Last edited by Barbara Morris; March 4, 2014, 06:15 PM.

              Comment


              • #8
                Re: Overlay fields for array.

                Originally posted by danlong005 View Post
                Code:
                         dcl-ds ids;
                            id1;
                            id2;
                            id3;
                            id4;
                            id5;
                            myids zoned(10:0) dim(5) [COLOR="#FF0000"]pos(1);[/COLOR]
                         End-Ds;
                Except it's always good to use LIKE for this kind of thing.
                Code:
                myids [COLOR="#FF0000"]like(id1)[/COLOR] dim(5) pos(1);

                Comment


                • #9
                  Re: Overlay fields for array.

                  The reason for the RNF4138 is that when you overlay an array, the overlaying field is also an array.

                  Code:
                  D ds              ds                                      
                  D   info                        10a   dim(10)             
                  D     x1                         5a   overlay(info)       
                  D     x2                         5a   overlay(info:*next) 
                   /free                                                    
                      info(1) = '1234567890';                               
                      dsply x1(1);                                          
                      dsply x2(1);                                          
                      return;
                  Code:
                  DSPLY  12345 
                  DSPLY  67890
                  In the example from the OP, id1 inherits the DIM(5) from overlay(myids), but since ID1 is an externally-described field, it can't be an array.
                  Code:
                       d                 ds
                       d myids                         10  0 dim(5)
                       d id1                           10  0 overlay(myids)

                  Comment

                  Working...
                  X