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: 

Need to read a field from flat file

Former Member
0 Kudos
101

Hi All,

In flat file i have one field which may contain 100 enteries separated by delimanator ',' (comma). There can be 1 , 10 or 100 enteries. I need to put them in the internal table . Please suggest me .

I have used the below logic .

i split the field in 100 variables and then appending the internal table . the problem here is even if there is only one entry , the logic will append 99 blank enteries . so can any one suggest , is there any other way do it . any Functon Modules available .

Thanks.

5 REPLIES 5

Former Member
0 Kudos
60

Create the internal table with Single Column (with 1024 characters) and upload the flat file data as it is in this internal table. After uploading the data in this internal table, loop through the table, use SPLIT command to split the data, append the non-initial values into the final internal table.

Regards

Vinod

Former Member
0 Kudos
60

Hi

Use this logic.

1. Let assume u have got data from file into internal table t_itab1.

2. Declare the internal table t_itab2 with only on field in it of lenght 40.

3. loop at t_itab1 into wa_itab1.

refresh t_itab2.

split wa_itab1 at ',' into table t_itab2.

(Now in t_itab2 internal table u will get the all fields,each field will come as separate row ).

if record as 10 fields then in table t_itab2 u will 10 record.

now process the t_itab2 and append as required.

Endloop.

0 Kudos
60

Hi Chetan,

If i understand your requirement it is as below,

Flat file.

1,2,3,4,5,6,7,8,9,....

1,2,3,4,5,6,7,8,9,....

1,,,,,,,,,,,,,,,,,,,,

Now you read the first line and split into an itab of type string, and you are worried about the third line where not all the fields have values and end up creating blank entries. to fix this, simply

Sort the Itab, and use delete itab statement with the where clause as where column is ''.

This should solve it.

types: begin of t_data,

line type string,

end of data.

data: li_file_line type standard table of t_data.

split l_file_line at ',' into li_file_line.

delete li_file_line where line = ''.

0 Kudos
60

TYPES : BEGIN OF text,

line TYPE c,

END OF text.

data:content TYPE TABLE OF text WITH HEADER LINE.

" Define another internal table work area then split

LOOP AT content.

SPLIT content AT cl_abap_char_utilities=>horizontal_tab INTO

wa_

wa_

APPEND wa_ TO it_.

Hope this will help u .

Former Member
0 Kudos
60

Check this code I made to see one posible aproach.

DATA:
entry(40) TYPE c VALUE '1,2,34,4,55,6,7,8,90,0,1,2,3',
len TYPE i,
count TYPE i,
flag TYPE c,
num(4) TYPE c,
BEGIN OF wa_tab,
  number TYPE i,
END OF wa_tab,
it_itab LIKE STANDARD TABLE OF wa_tab.

len = STRLEN( entry ).
flag = space.
count = 0.
WHILE flag = space.
  IF count < len.
    IF entry+count(1) = ','.
      wa_tab-number = num.
      APPEND wa_tab TO it_itab.
      CLEAR num.
    ELSE.
      CONCATENATE num entry+count(1) INTO num.
    ENDIF.
  ELSE.
    wa_tab-number = num.
    APPEND wa_tab TO it_itab.
    flag = 'X'.
  ENDIF.
  count = count + 1.
ENDWHILE.

LOOP AT it_itab INTO wa_tab.
  WRITE:/ wa_tab-number.
ENDLOOP.