ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Referencing array elements.

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

  • Referencing array elements.

    Hi,

    In an RPG IV program in free format, I want to read the elements of an array in a loop till the last element of the array is read. And I have to do some operations on each array element. The array elements are strings. But I don't know how to reference each array element in a loop.

    For example: If there are 2 arrays ResArray and StringArray of size 10. StringArray is an array containing 10 strings and I have to extract a part of each string in the array and put them in corresponding positions of ResArray. What will be the syntax to access an array element?

    /free

    DoU i = 10;
    ResArray[i] = %subst(StringArray[i]:4:2);
    i = i + 1;

    /end-free

    Can anyone let me know what will be the exact syntax for writing a code with the above logic?

    Thanks,
    Subha.

  • #2
    Re: Referencing array elements.

    This snipet works.

    D WHS S 70 DIM(10) CTDATA

    Dow X <= %ELEM(WHS);
    S1name = WHS(X);
    CustA = %SUBST(Whs(X):51:7);
    FwhsA = %SUBST(Whs(X):58:2);
    TwhsA = %SUBST(Whs(X):60:2);
    EndDo;

    Comment


    • #3
      Re: Referencing array elements.

      Use () instead of []

      Comment


      • #4
        Re: Referencing array elements.

        and this

        i = i + 1;

        can be

        i +=1;
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: Referencing array elements.

          and your missing an

          ENDDO;
          "Time passes, but sometimes it beats the <crap> out of you as it goes."

          Comment


          • #6
            Re: Referencing array elements.

            Hi,

            I'd use a FOR-Next-Loop

            PHP Code:
            /Free
               
            For Index 1 to %Elem(StringArray);
                 
            Result(Index) = %Subst(StringArray(Index): 24);
               EndFor;
            /
            End-Free 
            Birgitta

            Comment


            • #7
              Re: Referencing array elements.

              where i can get all funtions %...y for what use...?

              S.S.

              Comment


              • #8
                Re: Referencing array elements.

                v5r4


                v5r3
                All my answers were extracted from the "Big Dummy's Guide to the As400"
                and I take no responsibility for any of them.

                www.code400.com

                Comment


                • #9
                  Re: Referencing array elements.

                  Without using any loops, u can use %SUBARR function....

                  Example:::

                  D Array1 S 5A Dim(5)
                  D Array2 S 2A Dim(5)
                  D
                  /Free
                  Array1(1) = 'ABC ' ;
                  Array1(2) = 'DEF ' ;
                  Array1(3) = 'GHI ' ;
                  Array1(4) = 'JKL ' ;
                  Array1(5) = 'MNO ' ;
                  Clear Array2 ;

                  Array2 = %SubSt( %SubArr(Array1:1):1:2) ;
                  *Inlr = *On ;

                  /End-Free

                  Array2 will give

                  ARRAY2(1) = 'AB'
                  ARRAY2(2) = 'DE'
                  ARRAY2(3) = 'GH'
                  ARRAY2(4) = 'JK'
                  ARRAY2(5) = 'MN'

                  I think this is what ur requirement. With single line of code and with out using loops u can accomplish this .....
                  Thanks,
                  Giri

                  Comment


                  • #10
                    Re: Referencing array elements.

                    Gotta love it when Birgitta chimes in....

                    Short, sweet and to the point!

                    Comment


                    • #11
                      Re: Referencing array elements.

                      giri solution is unique and I will have to look into it.
                      Never trust a dog to watch your food.

                      Comment


                      • #12
                        Re: Referencing array elements.

                        HI

                        As Giri suggested: Array2 = %SubSt( %SubArr(Array1:1):1:2) ; .
                        You will get the same result with: Array2 = %SubArr(Array1:1) ;
                        LP Zdenko

                        Comment

                        Working...
                        X