Skip to Content

Nesting loops for unknown number or times

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.

it-dim-member.png (12.4 kB)
et-result1.png (15.7 kB)
Add a comment
10|10000 characters needed characters exceeded

  • 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
    
  • 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.

  • 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?

Related questions

2 Answers

  • Posted on Nov 06, 2019 at 05: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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 06, 2019 at 05:39 PM
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.