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: 

Nesting loops for unknown number or times

0 Kudos

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.

5 REPLIES 5

Sandra_Rossi
Active Contributor
0 Kudos

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?

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.

gdey_geminius
Contributor

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

cdprasanna
Active Participant

DoanManhQuynh
Active Contributor
0 Kudos

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