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

Efficient logic in a loop

Former Member
0 Likes
1,723

Hello all,

I have a program where I have logic something like


loop at itab.

   sort itab ascending by prueflos.

    clear   : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    refresh : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    CALL FUNCTION 'XYZ'
      EXPORTING
        I_PRUEFLOS    = itab-prueflos
      IMPORTING
        E_QALS        = t_qals
      TABLES
        T_QAMR        = t_qamr
        T_QAMV        = t_qamv
        T_QAPO        = t_qapo
        T_QASR        = t_qasr
        T_QAPP        = t_qapp
      EXCEPTIONS
        LOT_NOT_FOUND = 1
        OTHERS        = 2.


     if sy-subrc = 0.

      ............

     endif.

endloop.

The table itab can have duplicate prueflos. I don't want to run the FM XYZ for same inspection lots again and again as this FM takes considerable amount of time to run. For this I sorted the itab ascending by prueflos so that the duplicate prueflos would be one below the other. Now my idea is - if we come accross a duplicate prueflos, use the values t_qamr, t_qamv, t_qapo, t_qasr, t_qapp from previous iteration in the loop.

Could someone put there efficient ideas in. Let me know if something is not clear.

Thanks

17 REPLIES 17
Read only

Former Member
0 Likes
1,695

I think that's the way I would do it.

rob

Read only

0 Likes
1,695

Hi Rob,

I have my idea but I am struggling to put this idea in the code. Can me help me out. Thanks

Read only

0 Likes
1,695

Hi,

If you are have chance to edit the FM then you can create a internal table with in the function module, and you can write the logic inside the program by importing the entire internal table at one shot.

Regards,

  • Dj

reward, if its useful

Read only

0 Likes
1,695

Something like:


SORT itab ASCENDING BY prueflos.
LOOP AT itab.

  IF itab-prueflos = old_prueflos
*Logic to assign old value of variables here

  ELSE.

    old_prueflos = itab-prueflos.

    CLEAR   : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
  REFRESH : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    CALL FUNCTION 'XYZ'
      EXPORTING
        i_prueflos    = itab-prueflos
      IMPORTING
        e_qals        = t_qals
      TABLES
        t_qamr        = t_qamr
        t_qamv        = t_qamv
        t_qapo        = t_qapo
        t_qasr        = t_qasr
        t_qapp        = t_qapp
      EXCEPTIONS
        lot_not_found = 1
        OTHERS        = 2.


    IF sy-subrc = 0.



    ENDIF.

  ENDIF.

ENDLOOP.

Rob

Read only

0 Likes
1,695

You can as the others have suggested, copy to a second table. But then you'll likely have to copy the results back to the first table. I prefer the way I suggested, but the other way will work as well.

Rob

Read only

0 Likes
1,695

my mistake.... I meant to sort itab before loop over itab not in the loop. Now I would sort Itab and I do not want to loose any data in itab. Rob or Rich could you modify my code to show your ideas....

Read only

0 Likes
1,695

Sticking with the copy of the table here is the example.



* Here TTAB represents the type used to describe ITAB
data: itab2 type table of ttab with header line.

itab2[] = itab[].
sort itab2 ascending by prueflos.
delete adjacent duplicates from itab2 comparing prueflos.

loop at itab2.
 

    clear   : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    refresh : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    CALL FUNCTION 'XYZ'
      EXPORTING
        I_PRUEFLOS    = itab2-prueflos
      IMPORTING
        E_QALS        = t_qals
      TABLES
        T_QAMR        = t_qamr
        T_QAMV        = t_qamv
        T_QAPO        = t_qapo
        T_QASR        = t_qasr
        T_QAPP        = t_qapp
      EXCEPTIONS
        LOT_NOT_FOUND = 1
        OTHERS        = 2.
 
 
     if sy-subrc = 0.
 
      ............
 
     endif.
 
endloop.


Regards,

Rich Heilman

Some adjustments

Message was edited by:

Rich Heilman

Read only

0 Likes
1,695

Rob,

Your logic may not work for what I am asking for. If the itab-prueflos = old_prueflos then I have to do whatever I am doing after the FM ie., in the if sy-subrc = 0 loop.

Read only

0 Likes
1,695

I modified your code. The posting is somewhere above.

Rob

Read only

0 Likes
1,695

I'm assuming you are keeping everything you need from the FM call and can just move it in if the numbers are the same.

IE - at my comment.

Rob

Message was edited by:

Rob Burbank

Read only

0 Likes
1,695

Rich / Rob ,

May be I did not explain my requirement clearly. I have to loop on ITAB and do the steps 1, 2 and 3 for each and every prueflos where FM returns subrc 0. Here is my code again.



data : old_prueflos like qals-prueflos.

*  sort ITAB so the duplicates are one below the other
sort itab ascending by prueflos.   

clear : old_prueflos.
loop at itab.
  
    clear   : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    refresh : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    CALL FUNCTION 'XYZ'
      EXPORTING
        I_PRUEFLOS    = itab-prueflos
      IMPORTING
        E_QALS        = t_qals
      TABLES
        T_QAMR        = t_qamr
        T_QAMV        = t_qamv
        T_QAPO        = t_qapo
        T_QASR        = t_qasr
        T_QAPP        = t_qapp
      EXCEPTIONS
        LOT_NOT_FOUND = 1
        OTHERS        = 2.
 

     if sy-subrc = 0.
 
      step 1
      step 2
      step 3
 
     endif.
 
  old_prueflos = itab-prueflos.

endloop.

Now I need a logic something like - if old_prueflos = itab-Prueflos do not call FM and if FM return for this prueflos = 0 then do steps 1,2 and 3. If old_prueflos <> itab-Prueflos then call FM and if FM return is 0 do steps 1,2 and 3.

Something like this....

Read only

0 Likes
1,695

Raju,

If you use

DELETE ADJACENT DUPLICATES FROM itab2 COMPARING prueflos

you are removing the duplicates from itab2 (which is the copied table), so you won't be processing them twice - which should meet your requirements.

No need to keep a reference to the previous prueflos, since they will all be unique.

Read only

0 Likes
1,695

The logic I showed earlier would work, but this is better and includes your steps:


SORT itab ASCENDING BY prueflos.
LOOP AT itab.
 
  IF itab-prueflos <> old_prueflos

    old_prueflos = itab-prueflos.
 
    CLEAR   : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    REFRESH : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.
    CALL FUNCTION 'XYZ'
      EXPORTING
        i_prueflos    = itab-prueflos
      IMPORTING
        e_qals        = t_qals
      TABLES
        t_qamr        = t_qamr
        t_qamv        = t_qamv
        t_qapo        = t_qapo
        t_qasr        = t_qasr
        t_qapp        = t_qapp
      EXCEPTIONS
        lot_not_found = 1
        OTHERS        = 2.
 
    IF sy-subrc <> 0.
* Message?
    ENDIF.

  ENDIF.

*   STEP 1
*   STEP 2
*   STEP #
 
ENDLOOP.

Read only

Former Member
0 Likes
1,695

Some suggestions.

Do not sort the same itab you are looping inside of your loop. Sort once before you start looping.

After you sort, use

DELETE ADJACENT DUPLICATES FROM itab COMPARING prueflos.

and you'll have unique prueflos to loop through.

-


sort itab ascending by prueflos.

DELETE ADJACENT DUPLICATES FROM itab COMPARING prueflos.

loop at itab.

clear : t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.

refresh : t_qals, t_qamr, t_qamv, t_qapo, t_qasr, t_qapp.

CALL FUNCTION 'XYZ'

EXPORTING

I_PRUEFLOS = itab-prueflos

IMPORTING

E_QALS = t_qals

TABLES

T_QAMR = t_qamr

T_QAMV = t_qamv

T_QAPO = t_qapo

T_QASR = t_qasr

T_QAPP = t_qapp

EXCEPTIONS

LOT_NOT_FOUND = 1

OTHERS = 2.

if sy-subrc = 0.

............

endif.

endloop.

Read only

Former Member
0 Likes
1,695

Hi.........

You can use just after sort

DELETE ADJACENT DUPLICATES FROM itab compairing prueflos .

And if u dont want to make any changes in the original internal table, just make a temporary table, copy all data into that and then put the above query on it.

Hope this ll help

Thak-You

regards

vinsee

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,695

Like others have suggested, do the sort before going into the LOOP, but i am thinking that you could make a copy of ITAB into ITAB2 with the same structure, SORT ITAB2, then DELETE adjacement duplicates from ITAB2 and then LOOP at ITAB2, and call the function. This way, you have your original data in ITAB and you are using ITAB2 to call the function. Of course, if you do not need the original data, you could simply delete adjacent duplicates from ITAB. Anyway you go, you would want to do your SORT outside the LOOP just before going in.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,695

Its good to use AT NEW logic like below

Loop.

At new prueflos.

clear the variables.

call FM.....

Endat.

use the values returned by FM

Endloop.

AT NEW prueflos will be triggerred for evry new prueflos. You can clear all variables & call the function module .

Dont sort itab inside the loop.