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

Problem with nested loop

Former Member
0 Likes
883

I have been baffled by this problem from the last 2 days and ultimately i thought of bringing it here in this forum, if I could some guidance.

I have to fill a internal table with objects from another table.

for eg this is my internal table "all_lines_tab"

ID	               PRE	                            SUC
L000000001	                1	                       N000000001
L000000002	                1	                       N000000002
L000000004	      N000000001	                 N000000003
L000000005	      N000000001	                 N000000005
L000000006	      N000000001	                 N000000006
L000000012	      N000000002	                 N000000012
L000000009	      N000000002	                 N000000009
L000000003	      N000000003	                 N000000004
L000000008	      N000000005	                 N000000008
L000000007	      N000000006	                 N000000007
L000000010	      N000000009	                 N000000010
L000000011	      N000000009	                 N000000011
L000000013	      N000000012	                N000000013

now I have to arrange it in another internal table in the following pattern

Now I am looping at all_lines_tab with key pre = 1. ( this value of the key keeps changing)

I take the SUC value which is N000000001 and read table all_nvals_tab with key id = all_lines_tab-suc

I append the value in another table itab.

now this suc value becomes pre

so now pre is = N00000001

loop at all_lines_tab with with key pre ( which is now N0000001) , take the suc value ( which is now n0000003) and read all_nvals_tab with key id = all_lines_tab-suc (N0000003).

So in this way each suc value becames pre value in next loop pass.

The problem is each pre is present many times..for eg there are 3 pre which are n00000001 so the whole process has to be repeated 3 times etc which is resulting in lot of nested loop.

Can anyone please suggest how should I code for this....

if you are not able to understand the question revert back with your doubt

6 REPLIES 6
Read only

Former Member
0 Likes
836

Hi,

Use the delete statement inside the loop.

for ex:


loop at all_lines_tab into wa_all_lines_tab where pre = wa_all_lines_tab-pre .
delete all_lines_tab from wa_all_lines_tab.
endloop.

Then continue that main loop.

Hope this helps u.

thanks.

Read only

Former Member
0 Likes
836

Hi

Could you please paste the code you have written so that it can be analyzed and changed according to your need.

Regards,

Siddarth

Read only

0 Likes
836

this is my code

sort all_lines_tab1 by pre.
      loop at all_lines_tab1.
      a_tabix = sy-tabix.
      N = all_lines_tab1-pre.
        loop at all_lines_tab1 from a_tabix.
          IF ALL_LINES_TAB1-PRE = N.

            suc = all_lines_tab1-suc.
            clear sy-subrc.
            LOOP AT  all_nvals_tab where id = suc.
              if sy-subrc = 0.
                it_flowtab-id = all_nvals_tab-id.
                it_flowtab-fl = all_nvals_tab-fl.
                it_flowtab-val = all_nvals_tab-val.
                append it_flowtab.
              endif.
            endloop.

            N = suc.

          ENDIF.
        endloop.
      endloop.

but there is some problem with the code it is working for only the first chain n0000001 -> n00000003 -> n00000004

then again pre becomes n000000002 ( so the other two n00000001 values are descarded)

Read only

0 Likes
836

Hi,

sort all_lines_tab1 by pre.
      loop at all_lines_tab1.
            suc = all_lines_tab1-suc.
            clear sy-subrc.
            LOOP AT  all_nvals_tab where id = suc.
              if sy-subrc = 0.
                it_flowtab-id = all_nvals_tab-id.
                it_flowtab-fl = all_nvals_tab-fl.
                it_flowtab-val = all_nvals_tab-val.
                append it_flowtab.
              endif.
            endloop.
      endloop.

Please check if this meets your requirement ,

if not please do let me know what should be the output of the it_flowtab.

Regards,

Siddarth

Read only

faisalatsap
Active Contributor
0 Likes
836

Hi, Priya

Test the following Sample Code it is working according to you requirements hope will solve out your problem,

TYPES: BEGIN OF t_pre,
pre(10) ,
END OF t_pre.

TYPES: BEGIN OF t_suc,
  pre TYPE i,
  suc(10),
END OF t_suc.


DATA: it_pre TYPE STANDARD TABLE OF t_pre WITH HEADER LINE,
      it_suc TYPE STANDARD TABLE OF t_suc WITH HEADER LINE,
      count_loop LIKE sy-tabix.

it_suc-suc = 'N00001'.
DO 10 TIMES.
  it_pre-pre = sy-index.
  it_suc-pre = sy-index.

  APPEND: it_pre TO it_pre,
          it_suc TO it_suc.
  ADD 1 TO it_suc-suc+6(1).

ENDDO.

BREAK-POINT.
LOOP AT it_pre.
  count_loop = sy-tabix.
  READ TABLE it_suc INTO it_suc WITH KEY pre = it_pre-pre.
  IF sy-subrc EQ 0.
    it_pre-pre = it_suc-suc.
  ENDIF.

  MODIFY it_pre FROM it_pre.
ENDLOOP.

Please Reply if any Issue,

Kind Regards,

Faisal

Read only

Former Member
0 Likes
836

Hi,

let me first explain what i have understood from your question.

First PRE eq 1 and for that SUC eq N000..1.

you get all the details for this and append.

Then you PRE eq N0000...1 and for that SUC is N0000....3

you get all the details for this and append.

keep going.

I hope this is what you need.

Then try if this logic works,

Loop at all_line_tab where pre eq '1'.

SUC_VALUE = SUC (N000...1).

loop at all_line_tab where pre eq suc_value.

suc_value = suc.

endloop.

endloop.

I guess this might be of some little help.

If not give some more explanation and your Looping code so anybody here could fix it out.

Thanks,

Prashanth