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

How to control an infinite loop in recursion

former_member254358
Participant
0 Likes
2,616

Hi,

I have a BOM in which there are infinite loops, it means that a material can contains itself in the following level or in a deeper level.

For example,

(1, 2), (1, 3), (2, 4), (4, 1)

I have seen how recursive works in abap but I don't know how to control infinite loops.

Can anybody help me?

Thanks in advance.

Regards.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,178

Hi David

You should give us more details, I mean how you think to arrange your LOOP

Max

Hi,

I have a BOM in which there are infinite loops, it means that a material can contains itself in the following level or in a deeper level.

For example,

(1, 2), (1, 3), (2, 4), (4, 1)

I have seen how recursive works in abap but I don't know how to control infinite loops.

Can anybody help me?

Thanks in advance.

Regards.

7 REPLIES 7
Read only

Former Member
0 Likes
2,179

Hi David

You should give us more details, I mean how you think to arrange your LOOP

Max

Read only

0 Likes
2,178

Hi Max,

I'll try to explain in more detail what I'm looking for.

The loop shoul stop when:

1. I find a raw material (my raw materials begin with m or s)

2. I find a material which hasn't components

3. I find a material that is in the origin of the loop. For example, if I want the list of material of 1 (in the example) the loop shoul stop in the material 4 because it coints the 1 component.

Read only

0 Likes
2,178

Perhaps something like this could work:


DATA: BEGIN OF w_bom,

         matnr LIKE mast-matnr,

         werks LIKE mast-werks,

         stlnr LIKE mast-stlnr,

         items LIKE STANDARD TABLE OF stpo,

       END   OF w_bom.

DATA: t_bom LIKE STANDARD TABLE OF w_bom.

PARAMETERS: p_matnr LIKE mast-matnr,

             p_werks LIKE mast-werks.

PERFORM get_bom USING p_matnr p_werks.


FORM get_bom  USING    p1_matnr

                        p1_werks.

   DATA: l_mast TYPE mast.

   DATA: l_stpo TYPE stpo.

* Check if BOM material was loaded

   READ TABLE t_bom TRANSPORTING NO FIELDS WITH KEY matnr = p1_matnr

                                                    werks = p1_werks.

   CHECK sy-subrc <> 0.

* Check no raw material

   CHECK p1_matnr(1) <> 'M' AND p1_matnr <> 'S'.

* Check BOM

   SELECT * INTO l_mast

     FROM mast

       WHERE matnr = p1_matnr

         AND werks = p1_werks.

   ENDSELECT.

   CHECK sy-subrc = 0.

   CLEAR w_bom.

   FREE w_bom-items.

*

   w_bom-matnr = l_mast-matnr.

   w_bom-werks = l_mast-werks.

   w_bom-stlnr = l_mast-stlnr.

   SELECT * INTO TABLE w_bom-items

     FROM stpo

        WHERE stlty = 'M'

          AND stlnr = l_mast-stlnr.

   CHECK sy-subrc = 0.

   APPEND w_bom TO t_bom.

* Check next level

   LOOP AT w_bom-items INTO l_stpo.

     PERFORM get_bom USING l_stpo-idnrk l_mast-werks.

   ENDLOOP.

ENDFORM.      

The routine GET_BOM should call itself until the conditions to stop the search are satisfied, I can't try this code but you can try it or something like that.

Read only

0 Likes
2,178

Thanks Max for the example. I'll try the example with my BOM.

I haven't still run the example but, looking the code what I don't see is, where do you control the if the current material had appeared before in the loop?

I will use one of my materials with infinite loop to see what happens.

Thanks again.

Read only

0 Likes
2,178

* Check if BOM material was loaded

   READ TABLE t_bom TRANSPORTING NO FIELDS

          WITH KEY matnr =p1_matnr

                             werks = p1_werks.

   CHECK sy-subrc <> 0.

The main table T_BOM (or something like that) is to store the bom unread

If the current material is a bom read in previous level, the search will be stopped else continue


  w_bom-matnr = l_mast-matnr.

   w_bom-werks = l_mast-werks.

   w_bom-stlnr = l_mast-stlnr.

   SELECT * INTO TABLE w_bom-items

     FROM stpo

        WHERE stlty = 'M'

          AND stlnr = l_mast-stlnr.

   CHECK sy-subrc = 0.

   APPEND w_bom TO t_bom.

I mean it needs a support where to store the results of previous levels

Ma

Read only

rosenberg_eitan
Active Contributor
0 Likes
2,178

Hi,

Looks like chemical industry.

Try this:

Create internal table of matnr lets call it it_comp_1 .

Logic:

loop on root materials

for each:

- Clear it_comp_1 .

- Add the root matnr to it_comp_1 .

- When you traverse the tree fill check if the it_comp_1 contain the component if yes return without  going any deeper.

- if not add the component to it_comp_1 and go deeper.

For your knowledge there are standard functions that can be used,

Do a google using search operators :   site:scn.sap.com bom explosion functions

We use this: CS_BOM_EXPL_MAT_V2

Regards.

Read only

0 Likes
2,178

Thanks Eitan, the process it clear.

Regards.