ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Update file fields from array

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

  • Update file fields from array

    I have a file say MYFILE with fields fld1, …fld10
    I also have an array with elements elm1, …elm10

    How to properly declare MYFILE DS so I can populate all file fields with array values in one line of code? (Equivalent of MOVEA to MYFILE)

    Dcl-DS Array extname('MYFILE') qualified; ???

    Thanks
    Irene


  • #2
    Assuming all the FLD1 - FLD10 in your file are the same data type and that they are all together in the record, you can define an array FLDS for your Array data structure. Then you can assign your elm array to your Array.flds array.
    Code:
       Dcl-DS Array extname('MYFILE') qualified;
          flds like(Array.fld1) samepos(fld1) dim(10);
       End-Ds;
    
       Array.flds = elm;

    Comment


    • #3
      Or if the fields in MYFILE are not contiguous you can achieve the same thing by naming them in the DS like this:
      Code:
      Dcl-DS  ArrayData;
         fld1;
         fld2;
         ...
         fld10;
         array  like(fld1)  Dim(10)  Pos(1);
      End-DS;
      As with Barbara's solution this depends on fld1 - fld10 having the same definitions.

      Note that no data type definition is needed - just the names.

      Comment


      • #4
        Thanks,
        fields fld1...fld10 are all of the same char definition, but something is missing since I get Array.flds updated, but not the actual fields in file...​

        Comment


        • #5
          Show us your DS definition - it should work.

          Comment


          • #6
            Originally posted by JonBoy View Post
            Show us your DS definition - it should work.
            Code:
             Dcl-DS ItemArray extname('MYFILE') qualified;
                     FLDS like(ItemArray.FLD01) dim(10) pos(1);  ​
            ​
            MyFile has fields: ID, Fld01, …, Fld10
            Last edited by InfraRed; November 3, 2022, 01:34 PM.

            Comment


            • #7
              InfraRed, can you show us
              • the DCL-F for the file
              • the WRITE opcode where you are writing the data to the file

              Comment


              • #8
                Originally posted by Barbara Morris View Post
                InfraRed, can you show us
                • the DCL-F for the file
                • the WRITE opcode where you are writing the data to the file
                my understanding was that with EXTNAME declaration in DS we don't need to declare the file separately, however i tried with and without - same result:
                Code:
                Dcl-f MYFILE disk(*ext) usage(*update:*output) KEYED;
                Code:
                itemArray.FLDS = ITEMS;
                the result of array:
                ITEMARRAY.FLDS(1) = 'VF3'
                ITEMARRAY.FLDS(2) = 'WD1'
                ITEMARRAY.FLDS(3) = 'HD1'
                ...
                but actual file fields are not getting these values (like FLD01)

                Thanks for your help!​

                Comment


                • #9
                  Your array fields are not the fields from the record because you have not populated them.

                  Your Dcl-F is creating a set of fields that are separate from those in the DS. You could use Prefix on the file definition and point it to the ItemArray e.g. Prefix('ItemArray.')

                  Alternatively use the ItemArray DS as the target for the READ or CHAIN or whatever and use the same approach for the WRITE/UPDATE whatever.

                  Comment


                  • #10
                    Originally posted by JonBoy View Post
                    Prefix('ItemArray.')
                    Not valid.

                    Comment


                    • #11
                      Resolved:
                      Code:
                      Dcl-DS *n;
                      Fld1;
                      ...
                      Fld10;
                      ItmArray like(Fld1) dim(10) pos(1);
                      END-DS;
                      Code:
                      ItmArray = ITEMS;
                      However, if I want to ​skip fields declaration and use EXTFILE('MyFile'), i don't know how to point to a second field in the record, first being ID and not part in the array.
                      Something like this in fixed:

                      Click image for larger version  Name:	image.png Views:	0 Size:	2.7 KB ID:	158110

                      Click image for larger version  Name:	image.png Views:	0 Size:	6.0 KB ID:	158109

                      Thanks for all your help!​​
                      Last edited by InfraRed; November 7, 2022, 02:47 PM.

                      Comment


                      • #12
                        Originally posted by InfraRed View Post

                        Not valid.
                        Sorry - it probably needs to be uppercased as it is a literal. So Prefix('ITEMARRAY.')

                        Comment


                        • #13
                          As to your latest example - it is easy enough.
                          Code:
                          Dcl-DS  *N  EXTFILE('MYFILE');
                             ItmArray  like(Fld1)  dim(10)  samepos(Fld1);
                          End-DS;
                          Note that this only works if the fields are contiguous - if there are other fields between the array source fields then the "name them in a DS" approach discussed earlier is the easiest way.


                          Attached Files

                          Comment

                          Working...
                          X