ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Array Processing Help - Splitting Delimited Data

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

  • Array Processing Help - Splitting Delimited Data

    Hello All.

    Trying to figure out how to split a large string of delimited data in to a data structure where I can then address it via field names to make it a bit easier to work with the data.

    My input data is tilde delimited layout looks something like this:

    Customer~InvoiceNumber~InvoiceDate~InvoiceAmount

    There is more to it, but that's enough to paint the picture.

    I am using Scott's GETTOK tool to split the data in to an array so I end up with:

    InputArr(1) = Customer
    InputArr(2) = InvoiceNumber
    InputArr(3) = InvoiceDate
    InputArr(4) = InvoiceAmount

    The downside is then throughout the program, I end up needing to refer back to see "oh, what element is such and such data in?".

    Ideally what I would like to do is create an external file and then in the RPG program create a data structure that's patterned after that file (extname keyword).

    Then in my program, I'd have fields like this:

    INVRCD.CUST = Customer
    INVRCD.INVNO = InvoiceNumber
    INVRCD.INVDT = InvoiceDate
    INVRCD.INVAMT = InvoiceAmount

    That's much easier to read and manage in the program.

    But, what I can't seem to figure out is how to split the incoming data up and elegantly drop it in to the data structure.

    I am fine if the InputArr is defined as a bunch of alpha fields and likewise my PF (and ultimately data structure) is defined in the same way too so I can plunk the array data in to the DS, I just can't seem to figure out how to pull that off.

    Any help to shed some light on this is appreciated.

    Thank you.

  • #2
    That's the kind of job I use DATA-INTO for. This article references some others that contain code samples, one of which does basically what you need. http://ibmsystemsmag.com/Power-Syste...-data-with-rpg

    If you want a generic solution then consider adding appropriate column headings to the CSV and using that as input to my routine - OR - modify my example to pull an array of column names from the user supplied parameter to DATA-INTO instead. I think Scott has also published a basic CSV option for DATA-INTO as part of his CSVR4 routines but I haven't used it and don't know how flexible it is compared with mine.

    Comment


    • #3
      So why not code something like this:

      Code:
      INVRCD.CUST = InputArr(1);
      INVRCD.INVNO = InputArr(2);
      INVRCD.INVDT = InputArr(3);
      INVRCD.INVAMT = InputArr(4);
      Write a subprocedure that uses gettok() or data-into, or csvr4 or whatever you like to read the file, and then immediately assign to named fields like above, and return the value. Aside from that one routine, you don't ever need to refer to them by number again.

      Comment


      • #4
        Why not just create a file with DDS to define the data then
        CPYFRMIMPF FROMSTMF('/MyPath')
        TOFILE(FILE-A)
        MBROPT(*REPLACE)
        RCDDLM(*CRLF)
        FLDDLM('~')

        Then use FILE-A in program or am I missing something?

        Comment

        Working...
        X