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: 

How to slipt fields in a row of an external file and store in Table

Former Member
0 Kudos

Hi Experts,

I need to proces an External file and store into a table. Here need to Split the fields in a row and write as multiple rows of a table.

here i am giving the data sample ..

outlet1 type1 type2 type3

outlet2 type1 type3 type4 typ6 type7

The table should be as below.

outlet1 type1

outlet1 type2

outlet1 type2

outlet2 type1

outlet2 type3

outlet2 type4

outlet2 type6

outlet2 type7

Please give your suggesions and code sample that you have

Thanks

1 ACCEPTED SOLUTION

Mohamed_Mukhtar
Active Contributor
0 Kudos

hi,

try with the below code. This code is as per data given by u in your first thread.


DATA : it_string TYPE TABLE OF string,
       wa_string TYPE string,

       BEGIN OF it_itab OCCURS 0,
       f1(7),
       f2(5),
       END OF it_itab,

       fld TYPE TABLE OF string,
       wa TYPE string,
       wa1 TYPE string,
       cnt TYPE i VALUE 1.
       data lin TYPE i.

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                = 'C:\Documents and Settings\User\Desktop\abc.txt'
    filetype                = 'ASC'
    has_field_separator     = 'X'
  TABLES
    data_tab                = it_string
  EXCEPTIONS
    file_open_error         = 1
    file_read_error         = 2
    no_batch                = 3
    gui_refuse_filetransfer = 4
    invalid_type            = 5
    no_authority            = 6
    unknown_error           = 7
    bad_data_format         = 8
    header_not_allowed      = 9
    separator_not_allowed   = 10
    header_too_long         = 11
    unknown_dp_error        = 12
    access_denied           = 13
    dp_out_of_memory        = 14
    disk_full               = 15
    dp_timeout              = 16
    OTHERS                  = 17.
IF sy-subrc <> 0.
*  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

BREAK-POINT.

LOOP AT it_string INTO wa_string.
  cnt = 1.
  SPLIT wa_string AT space INTO TABLE fld.
  DESCRIBE TABLE fld LINES lin.
  LOOP AT fld INTO wa.
    cnt = cnt + 1.
    IF cnt <= lin.
      READ TABLE fld INTO wa INDEX 1.
      IF sy-subrc = 0.
        it_itab-f1 = wa.
      ENDIF.
      READ TABLE fld INTO wa1 INDEX cnt.
      IF sy-subrc = 0.
        it_itab-f2 = wa1.
      ENDIF.
      APPEND it_itab TO it_itab[].
    ENDIF.
  ENDLOOP.

ENDLOOP.

Thanks & Regards

10 REPLIES 10

Former Member
0 Kudos

loop at itab.

split itab-field at ' ' into v_value1 v_value2. <-- outlet1 type1 type2 type3

split v_value2 at ' ' into table itab1. <--

loop at itab1. This has 3 records as .. type1 type2 type3 ...

concatenate v_value1 itab1-field1 into it_output-data. this has outlet1 type1 , outlet1 type2 ... (for the first record .. similarly for the second ..)

append it_output.

endloop.

endloop.

store it_output into table ...

0 Kudos

Thanks for your reply .here the file is of type .CSV and the sample sile is given below.

outlet1 ,type1 , type2 ,type3 ,

outlet2 ,type1 ,type3 ,type4 ,typ6 ,type7

Former Member
0 Kudos

Hi,

Process your external file into an internal table as it is.

Eg : ITAB - Filed1 Field2 Field3 Field4 Filed5 Field6

outlet1 type1 type2 type3

outlet2 type1 type3 type4 typ6 type7

Use this method to determine the attributes of the internal table.

ref_descr ?= cl_abap_typedescr=>describe_by_data( ITAB ).

ITAB[] = ref_descr->components[].

Create a new internal table as per your requirement.

loop at itab into wa.

Use this statement to transfer the values of the field to field symbol

ASSIGN COMPONENT wa-field1 OF STRUCTURE 'ITAB' TO <fs>.

Append the values of the <fs> to new internal table.

endloop.

Thanx & Rgds,

Krishna

murat_kaya
Participant
0 Kudos

Hi,

first create an internal table like this:

DATA: BEGIN OF itab OCCURS 0,

out(10),

field01(10),

field02(10),

field03(10),

field04(10),

field05(10),

field06(10),

field07(10),

field08(10),

field09(10),

field10(10),

END OF itab.

get your data to this internal table

then create a second internal table and neccesary variables like this:

DATA: BEGIN OF gt_out OCCURS 0,

outlet(10),

field(10),

END OF gt_out.

DATA: l_field(10).

then use below code for conversion.

LOOP AT itab.

DO 10 TIMES VARYING l_field FROM itab-field01 NEXT itab-field02.

IF l_field IS INITIAL.

EXIT.

ELSE.

gt_out-outlet = itab-out.

gt_out-field = l_field.

APPEND gt_out.

CLEAR gt_out.

ENDIF.

ENDDO.

ENDLOOP.

this should work fine..

regards,

Murat Kaya

0 Kudos

Thanks for your reply, but in my case the number of fields in the file are not Static, it may vary .

my code will pick up dynamically. what should i do if i have 12 fields in some other rows of a file.

Thanks

murat_kaya
Participant
0 Kudos

Hi BhanuPrakash,

is there a max number of field that it may vary? if so how do you get this data to your internal table and what is the structure of your internal table? if you tell me these maybe i can help you more.

regards,

Murat Kaya

Edited by: Murat Kaya on Mar 20, 2009 1:28 PM

0 Kudos

your solution is right if i have fixed number of fields in my file. if my file has different number of fields for different rows it should not work.

the code should be Dynamic instead of static in number of fields in a row. Any how thanks for your solution and i have assigned points too.

Mohamed_Mukhtar
Active Contributor
0 Kudos

hi,

try with the below code. This code is as per data given by u in your first thread.


DATA : it_string TYPE TABLE OF string,
       wa_string TYPE string,

       BEGIN OF it_itab OCCURS 0,
       f1(7),
       f2(5),
       END OF it_itab,

       fld TYPE TABLE OF string,
       wa TYPE string,
       wa1 TYPE string,
       cnt TYPE i VALUE 1.
       data lin TYPE i.

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                = 'C:\Documents and Settings\User\Desktop\abc.txt'
    filetype                = 'ASC'
    has_field_separator     = 'X'
  TABLES
    data_tab                = it_string
  EXCEPTIONS
    file_open_error         = 1
    file_read_error         = 2
    no_batch                = 3
    gui_refuse_filetransfer = 4
    invalid_type            = 5
    no_authority            = 6
    unknown_error           = 7
    bad_data_format         = 8
    header_not_allowed      = 9
    separator_not_allowed   = 10
    header_too_long         = 11
    unknown_dp_error        = 12
    access_denied           = 13
    dp_out_of_memory        = 14
    disk_full               = 15
    dp_timeout              = 16
    OTHERS                  = 17.
IF sy-subrc <> 0.
*  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

BREAK-POINT.

LOOP AT it_string INTO wa_string.
  cnt = 1.
  SPLIT wa_string AT space INTO TABLE fld.
  DESCRIBE TABLE fld LINES lin.
  LOOP AT fld INTO wa.
    cnt = cnt + 1.
    IF cnt <= lin.
      READ TABLE fld INTO wa INDEX 1.
      IF sy-subrc = 0.
        it_itab-f1 = wa.
      ENDIF.
      READ TABLE fld INTO wa1 INDEX cnt.
      IF sy-subrc = 0.
        it_itab-f2 = wa1.
      ENDIF.
      APPEND it_itab TO it_itab[].
    ENDIF.
  ENDLOOP.

ENDLOOP.

Thanks & Regards

0 Kudos

hi learner,

This code is not working , the first internal table IT_STRING is not getting populted fields type1 , type 2.

after GUI upload the IT_STRING has the following entries only

outlet1

outlet2 it is not reading entaire row. sugggest me in this

0 Kudos

Hi learner,

your code is working fine , thnks for your help.

Thanks

Bhanuprakash.