Application Development and Automation 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: 
Read only

Split a string into different fields ofvariable lengths in a internal table

Former Member
0 Likes
11,850

Hello Everyone,

I have a question regarding splitting a string. I have searched in the forum for this. I did find some useful threads, but did not solve my problem.

I have nearly 80 fields in a structure.

I have a LCHR string where the data is seaprated by commas. I want to split this string into the 80 fields.

These 80 character fields have variable length. I read F1 on split. and was able to split the string at commas into a table , lets call itab which is of type table of string. But how to move this itab to my structure which has 80 fields each with different length? can it be done by using field symbols?

Appreciate your help!.

Thanks,

Rushi

1 ACCEPTED SOLUTION
Read only

GuyF
Active Participant
5,239

Hi,

You can use a code like this:


TYPES:  BEGIN OF ty_upload,
          data(1000)  TYPE c,
        END OF ty_upload.

TYPES:  ty_order          TYPE ztype, " Enter your own structure here
        tt_order          TYPE STANDARD TABLE OF ty_order,
        tt_upload         TYPE STANDARD TABLE OF ty_upload.

FORM split_order_data    USING it_upload    TYPE tt_upload
                      CHANGING et_order     TYPE tt_order.

  DATA: ls_upload     TYPE ty_upload,
        ls_order      TYPE ty_order,
        lt_split_data TYPE STANDARD TABLE OF string,
        ls_split_data TYPE string,
        lv_tabix      TYPE sy-tabix.

  FIELD-SYMBOLS: <fs_value> TYPE ANY.

  LOOP AT it_upload INTO ls_upload.
    SPLIT ls_upload AT ',' INTO TABLE lt_split_data.

    LOOP AT lt_split_data INTO ls_split_data.
      lv_tabix = sy-tabix.

      ASSIGN COMPONENT lv_tabix OF STRUCTURE ls_order
        TO <fs_value>.

      IF sy-subrc EQ 0.
        <fs_value> = ls_split_data.
      ENDIF.
    ENDLOOP.

    APPEND ls_order TO et_order.
  ENDLOOP.

ENDFORM.                    "split_order_data

The table IT_UPLOAD is the data I received from the file, and ET_ORDER is the data in my own structure.

Since I'm using a field symbol, the length of it doesn't matter. The only thing you need to worry about is that the type of the data is correct, that is - don't try to put strings in currency fields for example.

Let me know if you need more assistance.

Guy.

8 REPLIES 8
Read only

Former Member
0 Likes
5,239

If i understood your question correctly than you may refer =>

Read only

faisalatsap
Active Contributor
0 Likes
5,239

hi,

i think the following code will help you in this way,

loop at it_crow.

    clear: zfsl_stinfo.

    split it_crow at ',' into zfsl_stinfo-mandt  "here is the solution of you problem i think
                              zfsl_stinfo-st_id
                              zfsl_stinfo-st_n
                              zfsl_stinfo-st_fn
                              zfsl_stinfo-st_reg
                              zfsl_stinfo-st_ph
                              zfsl_stinfo-st_pm.
    insert zfsl_stinfo.

    if sy-subrc = 0 .
      commit work.
      count = count + 1.
    else.
      write: / , 'Insert Failed for', zfsl_stinfo-mandt,
                                      zfsl_stinfo-st_id,
                                      zfsl_stinfo-st_n,
                                      zfsl_stinfo-st_fn,
                                      zfsl_stinfo-st_reg,
                                      zfsl_stinfo-st_ph,
                                      zfsl_stinfo-st_pm..
    endif.
  endloop.
  write: count , 'Records were inserted'.

Replay if any problem,

Regards,

Faisal

Read only

Former Member
5,239

Hi,

Use the FMs STRING_SPLIT_AT_POSITION

or IQAPI_WORD_WRAP

and take that LCHAR field separatly and do your process by above function modules.

So it solves your problem.

Thanks

Read only

Former Member
0 Likes
5,239

Hello Amit,

Thank you for the suggestion. My problem is I have to split the long string into fields into a structure which has variable length. For example...the string is

aaaaaaaaaa,bbbbbb,ggg,tttttt

i want to split this into structure which has fields like this

order(30)

id(8)

rst(20)

type(10)

so i thght of using split statement. so that i can get like this

order = aaaaaaaaaa.

id = bbbbbb

rst = ggg

type = tttttt

but i have 80 fields like this.

So I don't want to write all the fields on the split statement as Faisal suggested.

According to that thread which Amit suggested, i think i have to give line length which is constant for all fields.

My problem still not solved...any more suggestions?

Thanks,

rushi

Read only

0 Likes
5,239

hi rushi... if any thing related to field-symbols I can think related to ur scenario is to use assign-components check the F1 help for it... and check if it might help u..

Read only

GuyF
Active Participant
5,240

Hi,

You can use a code like this:


TYPES:  BEGIN OF ty_upload,
          data(1000)  TYPE c,
        END OF ty_upload.

TYPES:  ty_order          TYPE ztype, " Enter your own structure here
        tt_order          TYPE STANDARD TABLE OF ty_order,
        tt_upload         TYPE STANDARD TABLE OF ty_upload.

FORM split_order_data    USING it_upload    TYPE tt_upload
                      CHANGING et_order     TYPE tt_order.

  DATA: ls_upload     TYPE ty_upload,
        ls_order      TYPE ty_order,
        lt_split_data TYPE STANDARD TABLE OF string,
        ls_split_data TYPE string,
        lv_tabix      TYPE sy-tabix.

  FIELD-SYMBOLS: <fs_value> TYPE ANY.

  LOOP AT it_upload INTO ls_upload.
    SPLIT ls_upload AT ',' INTO TABLE lt_split_data.

    LOOP AT lt_split_data INTO ls_split_data.
      lv_tabix = sy-tabix.

      ASSIGN COMPONENT lv_tabix OF STRUCTURE ls_order
        TO <fs_value>.

      IF sy-subrc EQ 0.
        <fs_value> = ls_split_data.
      ENDIF.
    ENDLOOP.

    APPEND ls_order TO et_order.
  ENDLOOP.

ENDFORM.                    "split_order_data

The table IT_UPLOAD is the data I received from the file, and ET_ORDER is the data in my own structure.

Since I'm using a field symbol, the length of it doesn't matter. The only thing you need to worry about is that the type of the data is correct, that is - don't try to put strings in currency fields for example.

Let me know if you need more assistance.

Guy.

Read only

Former Member
0 Likes
5,239

Thanks Guy,

My problem is solved!

Rushi

Read only

Former Member
0 Likes
5,239

Thanks Guy,

My problem is solved!

Rushi