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 do comparison with loop in internal table?

danny_loo
Participant
0 Likes
2,034

Hi all,

Thanks for spending the time reading my thread.

I need to do a data comparison in an internal table, comparing the 1st row to the 2nd row, 2nd row to the 3rd subsequently.

This is the code i have came out.

**

SORT ITAB ASCENDING BY ebeln ASCENDING.

loop at itab.

At first.

move itab-grmenge to total.

move-corresponding itab to gtab.

continue.

endat.

if gtab-ebeln = itab-ebeln and gtab-ebelp = itab-ebelp.

total = itab-grmenge + total.

move-corresponding itab to gtab.

else.

move total to gtab-grmenge.

append gtab.

clear total.

move itab-grmenge to total.

move-corresponding itab to gtab.

endif.

endloop.

**

I encounter a problem at the "at first" command.

When i test the code in steps, i noticed when it reached "at first", the itab becomes a wild card.

My intention is to copy the 1st row of data to my 2nd table(gtab),

go back the loop and compare the 1st row of data in 2nd table(gtab) with the 2nd row of data in 1st table(itab).

Please advise me how should i approach it, thanks alot.

1 ACCEPTED SOLUTION
Read only

anup_deshmukh4
Active Contributor
0 Likes
1,640

YOU have a SY-TABIX system variable by which you will get the the current index of itab in a loop you can catch it and then use

READ STATMENT WITH INDEX SY-TABIX + 1 , SY-TABIX +2 SO ON and so...on

Edited by: Anup Deshmukh on Jan 19, 2010 4:45 AM

5 REPLIES 5
Read only

anup_deshmukh4
Active Contributor
0 Likes
1,641

YOU have a SY-TABIX system variable by which you will get the the current index of itab in a loop you can catch it and then use

READ STATMENT WITH INDEX SY-TABIX + 1 , SY-TABIX +2 SO ON and so...on

Edited by: Anup Deshmukh on Jan 19, 2010 4:45 AM

Read only

former_member585060
Active Contributor
0 Likes
1,640

Hi,

With your logic, just check if a particular EBELN has more than one EBELP values, how does it work. By your logic the total of the first record will be added alway, is that you wanted.

Regarding the wildcard values, you can just simply move the workarea to another work area after LOOP statement.

LOOP AT itab.

wa_itab = itab. " wa_itab is same as itab.

.

.

.

.

ENDLOOP

Regards

Bala Krishna

Read only

Former Member
0 Likes
1,640

Hello,

Control break events (At First,At Last,At New, At End of) are events where if you call a field inside these events, the value is replaced by '*' for all fields which are on the right of the field on which the event is triggered.

Before entering the AT .... ENDAT assign the contents of itab into a temporary variable and assign the value of that temporary variable to the new internal table gtab. Also the value of grmenge is to be assigned to a variable and then you can use it to add to total.

For example if wa is a variable with a structure similar to itab and temp is a variable of type grmenge then before AT First

move itab-grmenge to temp

move itab to wa.

Now inside the AT....ENDAT

replace the itab-grmenge with temp and itab with wa. wa contains the actual vaues which will be passed to gtab rather than '*'.

Hope this helps in solvibg your issue.

Reagrds,

Sachin

Read only

danny_loo
Participant
0 Likes
1,640

Hi Anup, Bala and Sachin,

Thanks for the swift replies.

Let me try on the methods to see if it works out.

Edited by: calibertto on Jan 19, 2010 5:05 AM

Edited by: calibertto on Jan 19, 2010 5:05 AM

Read only

danny_loo
Participant
0 Likes
1,640

I've managed to solve the problem by adding the "read table index sy-tabix" in the AT statement.

Thank you for all your help.