Skip to Content

One of the requirements we had once was to read the data from string into a structure.
This kind of requirements can be there when we are dealing with data from unstructured sources right in ABAP.

One of the ways it can be done is described as below

1) Look for the field names in string, for example your string is

FIELD 1: TEST1 FIELD 2: TEST2 FIELD 3: TEST3 FIELD 4: TEST4 FIELD 5: TEST5 FIELD 6: TEST6 FIELD 7: TEST7 FIELD 8: TEST8 FIELD 9: TEST9 FIELD 10: TEST10

2) Create an SE11 structure, if there are spaces between the field names replace them with underscore(_) like below

3) Below is the code to break the string into structure

DATA: lv_field_name      TYPE string,
      lv_field_name_last TYPE string,
      lv_str             TYPE string, "string with field names and values
      lv_str1            TYPE string,
      lv_str2            TYPE string.

DATA: lr_struc     TYPE REF TO cl_abap_structdescr,
      ls_struct_ar TYPE ztest_structure_ar,
      lt_fields    TYPE ddfields.

DATA: lv_count     TYPE i,
      lv_field_num TYPE i.

FIELD-SYMBOLS: <fs_val>   TYPE any,
               <fs_field> TYPE dfies.

lv_str = |FIELD 1: TEST1 FIELD 2: TEST2 FIELD 3: TEST3 FIELD 4: TEST4 FIELD 5: TEST5 FIELD 6: TEST6 FIELD 7: TEST7 FIELD 8: TEST8 FIELD 9: TEST9 FIELD 10: TEST10|.

lr_struc ?= cl_abap_structdescr=>describe_by_data( ls_struct_ar ).
lt_fields = lr_struc->get_ddic_field_list( ).

DESCRIBE TABLE lt_fields LINES lv_field_num.

LOOP AT lt_fields ASSIGNING <fs_field>.
  CLEAR: lv_field_name, lv_str1, lv_str2.

  lv_count = lv_count + 1.
  lv_field_name = <fs_field>-fieldname.
  TRANSLATE lv_field_name USING '_ '.
  lv_field_name = |{ lv_field_name }:|.
  SPLIT lv_str AT lv_field_name INTO lv_str1 lv_str2.

  IF NOT lv_count EQ 1.
    SPLIT lv_str1 AT lv_field_name_last INTO lv_str1 lv_str2.
    REPLACE ':' IN lv_field_name_last WITH ''.
    TRANSLATE lv_field_name_last USING ' _'.
    ASSIGN COMPONENT lv_field_name_last OF STRUCTURE ls_struct_ar TO <fs_val>.
    CONDENSE lv_str2.
    <fs_val> =  lv_str2.
  ENDIF.

  lv_field_name_last = lv_field_name.

  IF lv_count EQ lv_field_num.
    SPLIT lv_str AT lv_field_name_last INTO lv_str1 lv_str2.
    REPLACE ':' IN lv_field_name_last WITH ''.
    TRANSLATE lv_field_name_last USING ' _'.
    ASSIGN COMPONENT lv_field_name_last OF STRUCTURE ls_struct_ar TO <fs_val>.
    CONDENSE lv_str2.
    <fs_val> =  lv_str2.
  ENDIF.
ENDLOOP.

Thanks,
Mohammad Arshid Ansari

 

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

    1. Mohammad Arshid Ansari Post author

      Hi Kiran,

      In the above example, the field name in ABAP structure is “FIELD_1” but in the text it is “FIELD 1:”. So we first read the field name from the structure and then make it same as field name in text by removing underscore “_” and adding colon “:” at the end. This statement adds the colon to the field name, it’s same as

      CONCATENATE lv_field_name ‘:’ INTO lv_field_name.

      Thanks
      Mohammad Arshid Ansari
       

      (1) 

Leave a Reply