ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to simulate multi dimension arrays

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

  • How to simulate multi dimension arrays

    I am charged with modifying a program that has 15 locns ('01', '02'.......'15'). The program already has an array to store the locns in. The array contain 15 elements of 2 characters each. Now I have to create an array of 100 tags for each locn. In theory, anyway, each element of the locn array will contain and it's own array of 100 elements containing a tag# for that locn. Sounds like a case for a multi dimensional array if RPGLE supported it. Outside of using a MODS with each occurrence containing an array of tags, what would be the best way to set this up

  • #2
    This example is pretty easy.

    Code:
           Dcl-DS Locations dim(15) qualified;
             locn    char(2);
             tags    char(10) dim(100);
           End-Ds;
    This gives an array - Locations. It contains a 2 character locn and tags which is a 100 element array. The data structure itself is an array of 15 elements.

    locations.locn(1) is the first locn - location.locn(15) would be the last. locations.tags(1,50) would be the 50th tag in the first location. Locations.tags(15,100) would be the 100th tag of the 15th location.

    Comment


    • #3
      Sounds like you haven't been keeping up-to-date on your RPG features. Multi-dimensional arrays have been available for OVER 16 YEARS! They came in with V5R2.

      Sounds like you need something like this:

      Code:
             dcl-ds locations dim(15) Qualified;
                location  char(2);
                dcl-ds  tags  dim(100);
                  tag  char(10);  // or however it is defined
                end-Ds;
             end-Ds;

      Comment


      • #4
        I believe I have the nomenclature wrong...

        locations(1).locn is the first locn - locations(15).locn would be the 15th. locations(1).tags(1) would be the first tag for the first locn - locations(1).tags(100) would be the 100th of the first locn, etc.

        Comment


        • #5
          And as always - I tend to make it much more difficult than necessary first go around...

          Code:
                 dcl-ds locations dim(15) qualified;
                   locn char(2);
                   tag  char(10) dim(100);
                 End-Ds locations;
          locations(1).locn is first locn of 15. locations(1).tag(1) is the first tag in the first location.

          Comment


          • #6
            Thanks for responding.

            Comment


            • #7
              Code:
               
                  dcl-ds locations dim(15) qualified;          locn char(2);          tag  char(10) dim(100);        End-Ds locations;
              So, if I want to zero out all the elements in the tag array for each element in locations array without blanking out the locn subfield in locations, is there a way to do it without having to loop throughlocations and doing a CLEAR tag or is a FOR loop the only option?

              Comment


              • #8
                Originally posted by gregwga50 View Post
                Code:
                dcl-ds locations dim(15) qualified;
                locn char(2);
                tag char(10) dim(100);
                End-Ds locations;
                So, if I want to zero out all the elements in the tag array for each element in locations array without blanking out the locn subfield in locations, is there a way to do it without having to loop throughlocations and doing a CLEAR tag or is a FOR loop the only option?
                Sorry about the CODE block, I would edit it if I could
                Last edited by mjhaston; July 16, 2019, 09:09 AM.

                Comment


                • #9
                  Super weird. Why was I able to edit your post? Or am I the only one who sees that?

                  I'm going to go back and edit a bunch of Jamie's posts and make him sound strange!
                  Your friends list is empty!

                  Comment


                  • #10
                    Originally posted by mjhaston View Post
                    Super weird. Why was I able to edit your post? Or am I the only one who sees that?
                    I don't have the ability to modify a post, even my own, that I know of. I've asked about this before but have never got an answer

                    Comment


                    • #11
                      I see your post like this. I find it completely odd that I can edit someone else's post. Do you see the code portion of your post with line breaks that it didn't have before?

                      Your friends list is empty!

                      Comment


                      • #12
                        Originally posted by mjhaston View Post
                        I see your post like this. I find it completely odd that I can edit someone else's post. Do you see the code portion of your post with line breaks that it didn't have before?
                        Yes, I do now...thanks. I still can't update my own posts, though. Not sure why, I've been on this board for a while now. I've seen some other poster complaining about not being able to update/delete a post of their own

                        Comment


                        • #13
                          Unfortunately it is not allowed to code like this
                          Code:
                          locations(*).tag(*) = '';
                          so the best way to do it is a loop
                          Code:
                          for x = 1 to %elem(locations);
                            locations(x).tag(*) = '';
                          endfor;
                          Regards
                          Peder

                          Comment

                          Working...
                          X