ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

%elem BIF - need suggestion

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

  • %elem BIF - need suggestion

    %elem return the number of elements of an array/table/ds array. Is there a BIF which gives the number of elements has values, I have an array with dimensions 999.

    If the data is present only in the first two elements, i want to run the loop only twice.

    I defined the loop as below,

    for i = 1 to %elem(array)
    {

    }

    This runs for 999 times even when the data is only in the first two elements. Please suggest better way to handle this.

    thanks, Vinoth

  • #2
    Code:
    For i = 1 to %elem(array);
      If array(i) = *blank;
        Leave;
      Endif;
    Endfor;
    Assuming your array doesn't have any skipped elements.

    Comment


    • #3
      Typically, you keep track of which elements you've loaded data into in a separate variable. So you might have a variable named 'loaded' which contains the count of the number of things you've loaded.

      Then you simply can loop up to that variable:
      Code:
      for i = 1 to loaded;
         ...
      endfor;
      In 7.4, IBM has added support for what is called "variable dimension arrays" where the system can keep track of how many elements you've loaded. In that case, %ELEM would only return the number loaded -- so I assume you're not using that.

      Comment


      • #4
        There is no BIF because the compiler (currently) has no way of knowing how many elements you have filled. Normally your code would count the number used while loading.

        That said - the V7.4 enhancement for supporting variable length arrays allows for %elem to return the active element count. See this http://ibmsystemsmag.com/Power-Syste...ion-arrays-rpg and this https://ibmsystemsmag.com/Power-Syst...mension-Arrays for more details.

        Comment


        • #5
          How did you populate your array? If you loaded it with sql, then the number of entries is in sqler3.
          Code:
          FETCH from LTxtCsr
          For 200 rows
          into :LTxtMods
          
                     // now print the TextBauSteine if there are any
          LTxtRecs = sqler3;
          for x2 = 1 to LTxtRecs;
          ....
          endfor;
          Regards

          Kit
          http://www.ecofitonline.com
          DeskfIT - ChangefIT - XrefIT
          ___________________________________
          There are only 3 kinds of people -
          Those that can count and those that can't.

          Comment


          • #6
            Originally posted by Scott Klement View Post
            In 7.4, IBM has added support for what is called "variable dimension arrays" where the system can keep track of how many elements you've loaded. In that case, %ELEM would only return the number loaded -- so I assume you're not using that.
            i've read this article concerning "variable dimension arrays" https://www.rpgpgm.com/2019/09/varia...ys-in-rpg.html
            thanks for the advice as i'm using arrays rather frequently

            Comment


            • #7
              We are still in 7.2

              I populated the array through loop. I used Scott suggestion by moving the count to a variable and used in my loop. Worked fine, will try %elem for active elements when we upgrade to 7.4.

              Might be irrelevant to the current topic - I faced the issue because of below condition - the array is loaded by below yajl_array loop.

              j =0;
              dow yajl_array_loop(dataNode:j:linenode);

              enddo;

              I didn't notice this initially, but found during debug. In the array loop, when line items are 2 - the J counter ends at 3. I mean the J is incremented after reading 2 line-items and then comes out of the loop as there is no third line item.









              Comment

              Working...
              X