11-06-2019 4:22 PM
Hi experts,
In this scenario, I have a table IT_DIM_MEMBER with 2 fields.
DIMENSION (C20) : dimension id
T_MEMBER (internal table): selected members for the dimension
Example:
Number of rows and values on rows of this table are determined at run time, based on system configuration.
Structure of result table (ET_RESULT) which I am trying to fill is dynamic, and dimensions listed on IT_DIM_MEMBER table are presented as columns. And in the end, it should be filled with all possible combinations of the values of T_MEMBER tables.
Is there a way to dynamically nest unknown number of loops (depending on the number of dimensions in IT_DIM_MEMBER table) so that I can go through all T_MEMBER combinations?
Or how can we achieve this?
Thanks,
Mehmet.
11-06-2019 5:05 PM
I don't get the question. What I understand is that you have this code:
LOOP AT it_dim_result REFERENCE INTO DATA(dim_result).
LOOP AT dim_result->t_member REFERENCE INTO DATA(member).
" Here, do what you want
What else do you need?
11-06-2019 5:12 PM
Hi Sandra,
No, the code you provide would loop in individual T_MEMBER tables.
The requirement I have is to get all cartesian combinations of T_MEMBER tables, that are available in IT_DIM_MEMBER. In above example I have 5 but it is determined at runtime.
I hope that clarifies.
Thanks,
Mehmet.
11-06-2019 5:29 PM
First you have to find out the field list of the result table. You can proceed as follows
FIELD-SYMBOLS: <fs_any> TYPE any.
data: lt_comp TYPE abap_component_tab.
DATA: lo_ref_wa TYPE REF TO cl_abap_structdescr.
"Append single line to result
APPEND INITIAL LINE TO et_result ASSIGNING <fs_any>.
"Get reference to the work area
lo_ref_wa ?= cl_abap_tabledescr=>describe_by_data( p_data = <fs_any> ).
"Get component list of work area -- et_result
lt_comp = lo_ref_wa->get_components( ).
Once you have found out the field list you can populate the result table as follows
data:
lv_index TYPE sy-tabix.
FIELD-SYMBOLS:
<fs_field> TYPE any.
v_index = 1.
"Loop for all the column of the ET_Result internal table
LOOP AT lt_comp INTO ls_comp.
read TABLE it_dim_member INTO data(ls_dim_member) with key dimension = ls_comp-name.
IF sy-subrc eq 0.
"Assign the respective column of the result table
assign COMPONENT ls_comp-name OF STRUCTURE <fs_any> to <fs_field>.
IF <fs_field> is ASSIGNED.
"Read a particular line of a dimension type
read TABLE ls_dim_member-t_member INTO data(dim_member) INDEX lv_index.
IF sy-subrc eq 0.
<fs_field> = dim_member.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.<br>
The above code will read 1st line of t_member of each dimension value.
Hope this helps you for finding out complete solution.
Thanks,
Gourab
11-06-2019 5:39 PM
Hi,
Check this
convert-internal-table-rows-into-columns-of
moving-columns-of-an-internal-table-to-rows-of-an
Regards,
Prasanna
Reward all helpful replies
11-07-2019 2:57 AM
I think you could achieve it with recursion, something like:
LOOP AT it_dim_result REFERENCE INTO DATA(dim_result).
LOOP AT dim_result->t_member REFERENCE INTO DATA(member).
final_wa = CORRESPONDING #( member ).
PERFORM get_next USING dim_result->dimension
sy-tabix.
ENDLOOP.
ENDLOOP.
FORM get_next USING dimension next_indx.
IF dimension = final_dimension. "Used RTTI to get
APPEND final_wa TO final_tab.
exit.
ENDIF.
LOOP AT it_dim_result REFERENCE INTO DATA(dim_result) FROM next_indx + 1.
LOOP AT dim_result->t_member REFERENCE INTO DATA(member).
final_wa = CORRESPONDING #( member ).
PERFORM get_next USING dim_result->dimension
sy-tabix.
ENDLOOP.
ENDLOOP.
ENDFORM