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: 

How to assign counter in the loop?

Former Member
0 Kudos

I have one internal table i_data with 100 records likeu2026.


Material	Plant
M1	P1
M2	P1
M3	P1
M4	P1
M5	P1
M6	P1
M7	P1
M8	P2
M9	P2
M10	P2
M11	P3
M12	P3
M13	P3
M14	P3
M15	P3
M16	P3
M17	P4
M18	P4
--------
--------

I have one selection screen parameter p_tot = 5.

I_data is sorted by plant.

While making loop at I_data, I want to check thatu2026

If counter > p_tot or plant changes,

Concatenate material with u2018_1u2019

Else

Use same material

Endif.

Here, for plant P1, if t counter is > 5 than use u2018_1u2019, then rest of two records use u2018_2u2019 then plant changes (P2) so use u2018_3u2019 then plant changes (P3) so use u2018_4u2019 here it is greater than 5 so for same plant (P3) for one record, use u2018_5u2019 then for plant (P4), use u2018_6u2019 and so on.

How to do this?

Regards,

ronny

1 ACCEPTED SOLUTION

peter_ruiz2
Active Contributor
0 Kudos

hi Ronny,

use this..


  DATA:
    ld_tabix TYPE sy-tabix,
    ld_count TYPE p,
    ld_ccnt  TYPE char10,
    ld_plant type waers.

  ld_tabix = 0.
  ld_count = 0.

  LOOP AT it_data INTO wa_data.
    if wa_data-plant ne ld_plant.
      ADD 1 TO ld_count.
      MOVE 0 TO ld_tabix.
    endif.

    ADD 1 TO ld_tabix.

    IF ld_tabix GT p_tot.
      ADD 1 TO ld_count.
      MOVE 1 TO ld_tabix.
    ENDIF.

    MOVE ld_count TO ld_ccnt.

    SHIFT ld_ccnt LEFT DELETING LEADING space.

    CONCATENATE wa_data-matrl '_' ld_ccnt INTO wa_data-matrl.
    CONDENSE wa_data-matrl.

    MODIFY it_data FROM wa_data INDEX sy-tabix.

    ld_plant = wa_data-plant.
  ENDLOOP.

regards,

Peter

13 REPLIES 13

Former Member
0 Kudos

hi,

you can use SY-TABIX as counter

0 Kudos

Please post the code example for it

0 Kudos

Hi,

Could you show your current code so that desired cahnges can be shown thru that example itself

Regards,

Surinder

Former Member
0 Kudos

HI,

you can use

either 1.SY-TABIX as counter

loop at  i_data  .   
          
       IF sy-tabix > 5         
         ..................
         ..................  --> your operations
       ENDIF.
        

       endloop.

or take a counter variable with type i and inlialise it to 0.

then.

loop at i_data.
        ADD 1 to counter.
          
       IF COUNT5ER > 5         
         ..................
         ..................  --> your operations
       ENDIF.
        

       endloop.

REGARDS,

ANIRBAN

Former Member
0 Kudos

Hi,

You can do something like this...

DATA concatenator type i default '1'.

constant: under_score type c value '_'.

IF sy-tabix GT p_tot.

CONCATENATE material under_score concatenator into material.

add 1 to concatenator.

ELSE.

AT NEW plant.

CONCATENATE material under_score concatenator into material.

add 1 to concatenator.

ENDAT.

ENDIF.

This will help You..

Regards,

Kunjal

Former Member
0 Kudos

Try like this..

counter = 0.

plnt_count = 1.

loop at l_data.

flag = 0.

at new plant.

counter = 0.

concatenate l_data-material '_' plnt_count into l_data-material.

UPDATE l_data (code to update internal table or whtever functionality u need).

plnt_count = plnt_count + 1.

flag = 1.

endat.

counter = counter + 1.

if counter > p_tot and flag = 0.

concatenate l_data-material '_1' into l_data-material.

UPDATE l_data (code to update internal table or whtever functionality u need).

plnt_count = plnt_count + 1.

endif.

endloop.

0 Kudos

Thnaks for response... but let me more specific...I need output like...


I need output likeu2026

Material	
M1_1		u201CP1
M2_1		u201CP1
M3_1		u201CP1
M4_1		u201CP1
M5_1		u201CP1
M6_2		u201CP1 u2013 More than V_TOT = 5 so joined with u2018_2u2019
M7_2		u201CP1 u2013 More than V_TOT = 5 so joined with u2018_2u2019
M8_3		u201CP2 u2013 Plant changes P1 to P2 so joined with u2018_3u2019
M9_3		u201CP2 
M10_3		u201CP2
M11_4		u201CP3 - Plant changes P2 to P3 so joined with u2018_4u2019
M12_4		u201CP3
M13_4		u201CP3
M14_4		u201CP3
M15_4		u201CP3
M16_5		u201CP3 - More than V_TOT = 5 so joined with u2018_5u2019
M17_6		u201CP4 - Plant changes P3 to P4 so joined with u2018_6u2019
M18_6 	   u201CP4

0 Kudos

Hi,

You can do something like this...

DATA concatenator type i default '1'.

constant: under_score type c value '_'.

IF sy-tabix EQ ( p_tot + 1).

CONCATENATE material under_score concatenator into material.

add 1 to concatenator.

ELSE.

AT NEW plant.

CONCATENATE material under_score concatenator into material.

add 1 to concatenator.

ENDAT.

ENDIF.

This will work for you....

Regards,

Kunjal

Former Member
0 Kudos

try making ranges for these type of problems.

peter_ruiz2
Active Contributor
0 Kudos

hi Ronny,

use this..


  DATA:
    ld_tabix TYPE sy-tabix,
    ld_count TYPE p,
    ld_ccnt  TYPE char10,
    ld_plant type waers.

  ld_tabix = 0.
  ld_count = 0.

  LOOP AT it_data INTO wa_data.
    if wa_data-plant ne ld_plant.
      ADD 1 TO ld_count.
      MOVE 0 TO ld_tabix.
    endif.

    ADD 1 TO ld_tabix.

    IF ld_tabix GT p_tot.
      ADD 1 TO ld_count.
      MOVE 1 TO ld_tabix.
    ENDIF.

    MOVE ld_count TO ld_ccnt.

    SHIFT ld_ccnt LEFT DELETING LEADING space.

    CONCATENATE wa_data-matrl '_' ld_ccnt INTO wa_data-matrl.
    CONDENSE wa_data-matrl.

    MODIFY it_data FROM wa_data INDEX sy-tabix.

    ld_plant = wa_data-plant.
  ENDLOOP.

regards,

Peter

Former Member
0 Kudos

hello,

just copy paste this code and check if that wat u need.... similarly u can do in ur code..

DATA : BEGIN OF tab OCCURS 0,

plant TYPE char2,

mat TYPE char6,

END OF tab.

data : counter type i.

tab-mat = 'M1'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M2'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M3'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M4'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M5'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M6'.

tab-plant = 'P1'.

append tab.

tab-mat = 'M7'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M8'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M9'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M10'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M11'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M12'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M13'.

tab-plant = 'P3'.

append tab.

tab-mat = 'M14'.

tab-plant = 'P2'.

append tab.

tab-mat = 'M15'.

tab-plant = 'P2'.

append tab.

tab-mat = 'M16'.

tab-plant = 'P2'.

append tab.

sort tab by plant.

loop at tab.

at new plant.

skip 2.

counter = 1.

endat.

if counter > 5.

concatenate tab-mat '_01' into tab-mat.

modify tab.

endif.

counter = counter + 1.

endloop.

loop at tab.

write : / tab-mat , 15 tab-plant.

endloop.

0 Kudos

small change for plant counter

replace the logic by this code.....

data : plnt_cnt type char1.

sort tab by plant.

loop at tab.

at new plant.

skip 2.

counter = 1.

plnt_cnt = tab-plant+1(1).

endat.

if counter > 5.

concatenate tab-mat '_0' plnt_cnt into tab-mat.

modify tab.

endif.

counter = counter + 1.

endloop.

Former Member
0 Kudos

Hi this will work..

data:

begin of l_data occurs 0,

material(10) type c,

plant(5) type c,

end of l_data,

counter type i,

plnt_count type i,

plnt_count1(5) type c,

flag.

counter = 0.

plnt_count = 0.

loop at l_data.

flag = 0.

on change of l_data-plant.

counter = 0.

plnt_count = plnt_count + 1.

move plnt_count to plnt_count1.

condense plnt_count1.

flag = 1.

endon.

counter = counter + 1.

if counter > 5 and flag = 0.

plnt_count = plnt_count + 1.

move plnt_count to plnt_count1.

condense plnt_count1.

flag = 1.

counter = 0.

endif.

concatenate l_data-material '_' plnt_count1 into l_data-material.

modify l_data index sy-tabix.

endloop.