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: 

Avoid Nested Loops for non-unique internal tables (using ABAP 7.4)

sankar1781
Participant
0 Kudos
756

Hi Experts,

I am working on Updating Material Text using MATMAS interface.

In that, there are multiple Sales area segments, considered as one internal table and multiple long Text segments, considered as another internal table. Both doesn't have any unique value but all the texts (Material master texts) must be mapped / extended to all the Sales area.

For ex: Sales area segment records would be 8 entries and the Material long text entries would be 5.

Since there is no unique key to validate or match the entries between the two, using the parallel cursor technique to avoid nested loops cannot be achieved (as per my understanding).

Please help me out with your valuable solution using ABAP 7.4 or avoid a performance issue.

Thanks.

1 ACCEPTED SOLUTION

sankar1781
Participant
0 Kudos
624

SORT itab_1 BY key_1.

SORT itab_2 BY key_2.
index_2 = 0.
LOOP AT itab_1 INTO line_1.
  WHILE index_2 < lines( itab_2 )
      AND ( index_2 = 0 
         OR line_2-key_2 < line_1-key_1 ).
    index_2 = index_2 + 1.
    line_2 = itab_2[ index_2 ].
  ENDWHILE.
  IF line_1-key_1 = line_2-key_2.
    " Processing
  ENDIF.
ENDLOOP.
6 REPLIES 6

Sandra_Rossi
Active Contributor
624

Why do you want to change your code? Is there any performance issue?

FYI, Constructor Expressions are not faster than classic ABAP (note sure where this myth comes from...)

Sandra_Rossi
Active Contributor
624

Please use the COMMENT button for comments, asking for complements, adding details, replying to a comment or a proposed solution or to the OP question, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area.

Sandra_Rossi
Active Contributor
624

Note that there's no reason to avoid nested loops for the reason that the internal tables have non-unique key.

Also, the "parallel cursor technique" is based on using nested loops, so it's difficult to do the technique and avoid nested loops. Reminder (pseudo code of one possible variant):

SORT itab_1 BY key_1.
SORT itab_2 BY key_2.
index_2 = 0.
LOOP AT itab_1 INTO line_1.
  WHILE index_2 < lines( itab_2 )
      AND ( index_2 = 0 
         OR line_2-key_2 < line_1-key_1 ).
    index_2 = index_2 + 1.
    line_2 = itab_2[ index_2 ].
  ENDWHILE.
  IF line_1-key_1 = line_2-key_2.
    " Processing
  ENDIF.
ENDLOOP.

sankar1781
Participant
0 Kudos
624

Hi sandra.rossi

Thanks for your solution. This answered to my question.

sankar1781
Participant
624

Hi sandra.rossi,

Thanks again for your prompt response.

The problem here is parallel cursor cannot be used and directly to use two loops as nested loops, since both doesn't have any unique key to validate.

I can't change the data declaration to have an indicator because the two segment entry counts doesn't match as well.

For ex: Sales area segment may have 10 entries but the Text segment could be 1 or 2.

So why I asked a question here.

Thanks again for clearing my myth as well.

sankar1781
Participant
0 Kudos
625

SORT itab_1 BY key_1.

SORT itab_2 BY key_2.
index_2 = 0.
LOOP AT itab_1 INTO line_1.
  WHILE index_2 < lines( itab_2 )
      AND ( index_2 = 0 
         OR line_2-key_2 < line_1-key_1 ).
    index_2 = index_2 + 1.
    line_2 = itab_2[ index_2 ].
  ENDWHILE.
  IF line_1-key_1 = line_2-key_2.
    " Processing
  ENDIF.
ENDLOOP.