Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

split delimited file into multiple structures

Former Member
0 Kudos

Hi experts,

I have a delimited text file that has multiple row types - each row contains only one structure, but there could be 10 different row types (structures) in the file. I can figure out which structure each row belongs. Just need to come up with a way to dynamically split each line.

I am trying to stay away from:

case 'row type'

when 'structure A'

split into A1, A2, A3...

when 'structure B'

split into B1, B2, B3...

...

end with.

Rather, create a routine accepting file line and structure (or structure name) that returns the structure with data populated.

Any suggestions?

Thanks,

Hyun Kang

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

This may give you some ideas.



report zrich_0001.

data: begin of itab1 occurs 0,
      fld1(10) type c,
      end of itab1.

data: begin of itab2 occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      end of itab2.

data: begin of itab3 occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      fld3(10) type c,
      end of itab3.

data: tab_name type string.
data: istr type table of string with header line.
data: isplit type table of string with header line.

field-symbols: <dyn_tab> type table,
               <dyn_wa>,
               <fs>.


start-of-selection.

  call function 'GUI_UPLOAD'
       exporting
            filename = 'C:test.txt'
       tables
            data_tab = istr.


  loop at istr.
    split istr at ',' into table isplit.
    read table isplit index 1.

    concatenate isplit '[]' into tab_name.
    assign (tab_name) to <dyn_tab>.
    assign (isplit) to <dyn_wa>.
    delete isplit index 1.

    loop at isplit.
      assign component sy-tabix of structure <dyn_wa> to <fs>.
      if sy-subrc <> 0.
        exit.
      endif.
      <fs> = isplit.
    endloop.
    append <dyn_wa> to <dyn_tab>.
  endloop.


  loop at itab1.
    write:/ itab1-fld1.
  endloop.

  loop at itab2.
    write:/ itab2-fld1, itab2-fld2.
  endloop.

  loop at itab3.
    write:/ itab3-fld1, itab3-fld2, itab3-fld3.
  endloop.

My file looks like this.




ITAB1,Value1
ITAB1,Value2
ITAB2,ValueA,ValueB,
ITAB2,ValueC,ValueD,
ITAB3,ValueR,ValueS,ValueT
ITAB3,ValueU,ValueV,ValueW



You can see in this program, that the first column drives what internal table the data is written to for that line.

Regards,

RIch Heilman

4 REPLIES 4

Former Member
0 Kudos

I think u need to create a routine i which u'll have to pass original structure type of structure... and a table to contain splitted value..

then dynamically u can check....

Form split_str tables itab type string using org_str str_type.

endform.

<b>can u plz send one sample to check what type structure and type u r going to use..</b>

Regards

Prax

Former Member
0 Kudos

Hi,

Define split_file.

.... split &1 ... file

end-of-definition.

split_file <file> <structure>.

Regards,

Srilatha.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

This may give you some ideas.



report zrich_0001.

data: begin of itab1 occurs 0,
      fld1(10) type c,
      end of itab1.

data: begin of itab2 occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      end of itab2.

data: begin of itab3 occurs 0,
      fld1(10) type c,
      fld2(10) type c,
      fld3(10) type c,
      end of itab3.

data: tab_name type string.
data: istr type table of string with header line.
data: isplit type table of string with header line.

field-symbols: <dyn_tab> type table,
               <dyn_wa>,
               <fs>.


start-of-selection.

  call function 'GUI_UPLOAD'
       exporting
            filename = 'C:test.txt'
       tables
            data_tab = istr.


  loop at istr.
    split istr at ',' into table isplit.
    read table isplit index 1.

    concatenate isplit '[]' into tab_name.
    assign (tab_name) to <dyn_tab>.
    assign (isplit) to <dyn_wa>.
    delete isplit index 1.

    loop at isplit.
      assign component sy-tabix of structure <dyn_wa> to <fs>.
      if sy-subrc <> 0.
        exit.
      endif.
      <fs> = isplit.
    endloop.
    append <dyn_wa> to <dyn_tab>.
  endloop.


  loop at itab1.
    write:/ itab1-fld1.
  endloop.

  loop at itab2.
    write:/ itab2-fld1, itab2-fld2.
  endloop.

  loop at itab3.
    write:/ itab3-fld1, itab3-fld2, itab3-fld3.
  endloop.

My file looks like this.




ITAB1,Value1
ITAB1,Value2
ITAB2,ValueA,ValueB,
ITAB2,ValueC,ValueD,
ITAB3,ValueR,ValueS,ValueT
ITAB3,ValueU,ValueV,ValueW



You can see in this program, that the first column drives what internal table the data is written to for that line.

Regards,

RIch Heilman

0 Kudos

Bang on, thanks Rich.