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 STRING INTO INTERNAL TABLE WITH MULTIPLE FIELDS

former_member811846
Discoverer
0 Likes
2,884

How can I split this string matnr='RM235',mtart='RAME',ernam='ROE' in an internal table of type key, values??

I tried with the following code but it only works with just one field like matnr='RM235'. When I specify more fields separated by comma, it does not work anymore.

    DO.
TRY.
DATA(lv_segment_field) = segment( val = iv_param index = sy-index sep = ',' ). "index = sy-index
DATA(lv_value_where) = segment( val = iv_param index = 1 sep = iv_separator ).
APPEND VALUE #( field_name = to_upper( lv_segment_field ) ) TO zaml_dynamic_table_api=>gt_segments_fields.
LOOP AT zaml_dynamic_table_api=>gt_segments_fields INTO DATA(ls_segments).
*TRUE IF SPECIFIED ONE FIELD /SPFLI/CARRID='SQ'
IF ls_segments-field_name CA '='.
DATA(lv_segment_field_where_field) = segment( val = iv_param index = sy-index sep = '=' ).
DATA(lv_segment_field_where_val) = segment( val = iv_param index = sy-index + 1 sep = '=' ).
APPEND VALUE #( field_name = to_upper( lv_segment_field_where_field ) value = lv_segment_field_where_val )
TO zaml_dynamic_table_api=>gt_segments_where.



ENDIF.

ENDLOOP.
CATCH cx_sy_strg_par_val.
EXIT.
ENDTRY.
ENDDO.
2 REPLIES 2
Read only

Sandra_Rossi
Active Contributor
0 Likes
2,556

You can use SPLIT:

SPLIT iv_param INTO TABLE DATA(segments).
Read only

touzik_itc
Active Participant
2,556
TYPES: BEGIN OF t_key_value,
         key   TYPE string,
         value TYPE string,
       END OF t_key_value.
TYPES tt_key_value TYPE TABLE OF t_key_value WITH EMPTY KEY.
DATA(s) = |matnr='RM235',mtart='RAME',ernam='ROE'|.

SPLIT s AT ',' INTO TABLE DATA(lt_params).
DATA(lt_key_value) = VALUE tt_key_value( FOR <p> IN lt_params
  ( key = substring_before( val = <p> sub = '=' )
    value = condense( val = substring_after( val = <p> sub = '=' ) del = |'| ) ) ).

cl_demo_output=>display( lt_key_value ).

Result:

KEY   VALUE
matnr RM235
mtart RAME
ernam ROE