ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Data Structure and Array

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

  • Data Structure and Array

    I have combine 10 numeric fields (9,3) to a Data Structure. So the Data Structure have 90 length.
    Then I create an Array with DIM(10), each element is (9,3).
    I want to move the Data Structure to the array with operation 'MOVEA'.
    Finally i want to EVAL the Array(x) to a new field.
    How I need to code for the RPGLE? Hope you can help me.

  • #2
    r u looking for something like this???

    example
    Code:
         
         D* The file BUDGET hold budget info both $dollars and expected sold qtys
         D* We just want the money, so it starts in position 6 and ends with 77.
         D*  Its also stored by month (thats why 12) so now we have all $'s for all
         D* months in one place BUD 
         D*
         D               E DS                  EXTNAME(BUDGET)
         D  BUD                    6     77P 2
         D                                     DIM(12)
    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


    • #3
      Maybe u can do something like this,
      this example is for string it will work for decimals.

      Code:
      D testds      ds                                                                             
      D  arrayds                   76    dim(99) ascend                                             
      D  array1                     1    overlay(arrayds:1)                
      D  array2                     12   overlay(arrayds:2)                
      D  array3                     45   overlay(arrayds:14)               
      D  array4                     15   overlay(arrayds:59)              
      D  array5                     1    overlay(arrayds:75)               
      D  array6                     1    overlay(arrayds:76)               
      D  array7                     13   overlay(arrayds:1)
      "It's like a koala pooped a rainbow on my head and I can taste the colors."

      Comment


      • #4
        Hi all,

        Iam facing same issue for above kind of move operation between decimal DS and decimal array.



        #
        D posDS ds
        D pos1 2 0
        D pos2 2 0
        D posArr 2 0 dim(2)
        C MoveA posDS posARR
        #

        In the above code I am getting RNF7262 - Factor 2 and Result field are not same type and length. Kindly assist in what going wrong as data type and size are defined correctly.

        Comment


        • #5
          MOVEA is the legacy** opcode for copying one array into another array. posDS is not an array, therefore cannot be used with a DS.
          (**legacy in that it still works, but has no free format equivalent and IBM recommend you use the %SUBARR() function or a loop instead).

          You have coded posArr as an extra field at the end of the DS, so it is part of the DS, not a separate field

          When you have a DS that's a list of fields with the same data type and length, you can include an array inside the DS that overlays it, so each field in the DS is al;so an array element. Then your array of elements is automatically defined for you

          Code:
               D posDs           DS
               D  pos1                          2s 0
               D  pos2                               like(pos1)
               D  pos3                               like(pos1)
               D  pos4                               like(pos1)
               D  posArr1                1      8s 0 dim(4)
          
               D posArr2         S              2s 0 dim(%elem(posArr1))
               D posArr3         S              2s 0 dim(5)
               D posArr4         S              4s 0 dim(5)
          
               D a               S              2p 0
                * If from and to arrays have the same number of elements
                * and the same data type/size, can directly assign
               C                   eval      posArr2 = posArr1
          
                * If to array has less elements than from array,  use %subarr
               C                   eval      posArr2 = %subarr(posArr3:1:2)
          
                * If the two arrays are different data types, use a loop
               C                   for       a = 1 to %elem(posArr1) by 1
               C                   eval      posArr2(a) = posArr1(a)
               C                   endFor
          The reason that posArr1 says it's length 8 instead of length 2, is I'm actually defining the portion of the DS that the array takes up (from byte 1 to byte 8) Rather than the size of one element (2 digits). Because it's 8 bytes long an is an array of 4, each element is actually 2 bytes (8/4), which is the size of a single 2 digit zoned field.

          But these days, it's advisable for many reasons to use free format RPGLE instead of fixed format. That also simplifies the DS definition. This code is functionally identical, just specified in free format instead:
          Code:
                 dcl-Ds posDs;
                   pos1         zoned(2);
                   pos2         like(pos1);
                   pos3         like(pos1);
                   pos4         like(pos1);
                   posArr1      like(pos1) pos(1) dim(4);
                 end-Ds;
          
                 dcl-S posArr2 like(pos1) dim(%elem(posArr1));
                 dcl-S posArr3 like(pos1) dim(5);
                 dcl-S posArr4 zoned(4)   dim(5);
          
                 dcl-S a       packed(2);
          
                 // If from and to arrays have the same number of elements
                 // and the same data type/size, can directly assign
                 posArr2 = posArr1;
          
                 // If to array has less elements than from array, use %subarr
                 posArr2 = %subarr(posArr3:1:2);
          
                 // If the two arrays are different data types, use a loop
                 for a = 1 to %elem(posArr1) by 1;
                   posArr2(a) = posArr1(a);
                 endFor;


          Comment

          Working...
          X