10-21-2008 5:55 AM
Hi,
I have an internal table with 5 columns
Field1 Field2
AAA BB ......
AAA CC
BBB CC
DDD CC
Now I am using
LOOP AT itab.
AT NEW Field2.
ADD 1 TO COUNTER.
ENDAT.
ENDLOOP.
Now consider first row, since BB is new
COUNTER = 1.
Then consider second record, since CC is new
COUNTER = COUNTER +1 = 2
Then consider third record, since CC is not new, I do not want 1 to be added to counter, however in DEBUG i noticed that since Field1 = AAA for second record <> Field1 = BBB for third record (Field CC is same in second and third record),
COUNTER = COUNTER +1 = 3.
But I do not want this to happen.
FOR the following internal table , counter should be 2, however in debug, I noticed that counter = 4, i want to change counter only on change of field 2, how can I do this, please note that my table is an internal table and is not a data set.
AAA BB ......
AAA CC
BBB CC
DDD CC
10-21-2008 6:22 AM
Hi MN,
Do like this,
LOOP AT itab.
on change of Field2.
ADD 1 TO COUNTER.
endon.
ENDLOOP.
Kiran
10-21-2008 5:57 AM
Hi,
For perfect results place Field2 as Field1 in your internal table. and use At new Field1.
Thanks & Regards,
Navneeth K.
10-21-2008 5:59 AM
Hi ,
change ur internal table in such a way that u keep ur field2 first then field1.
like field2 field1.
BB AAA
CC AAA
CC BBB
CC DDD
now loop at itab.
at new field2..
...
endat.
now check the counter ...
Rgds.,
subash
10-21-2008 6:01 AM
Hi,
When u use AT NEW <field> it considers the fields before the <field>. So any change on the fields on the left hand side of <field> will trigger the AT NEW <field> statement.
So in ur case keep the Field2 at the beginning of the table.
This will solve ur problem...
10-21-2008 6:01 AM
10-21-2008 6:02 AM
hi,
as per ur case :
restructure ur table as FIELD2 FIELD1 and then sort it on the basis of FIELD2.and in the loop use AT NEW FIELD2.....now it will work.....
10-21-2008 6:02 AM
Hi
I think you have declared the internal table structure as follows
DATA :
BEGIN OF fs_tab,
field1 TYPE <xyz>,
field2 TYPE <xyz>,
END OF fs_tab.
Instead of that you declare the internal table as follows
DATA :
BEGIN OF fs_tab,
field2 TYPE <xyz>,
field1 TYPE <xyz>,
END OF fs_tab.
and try your issue will be solved
10-21-2008 6:02 AM
Check below code:
DATA:
BEGIN OF itab OCCURS 0,
field1(2),
field2(3),
END OF itab.
DATA:
counter TYPE i.
itab-field2 = 'AAA'.
itab-field1 = 'BB'.
APPEND itab.
CLEAR itab.
itab-field2 = 'AAA'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.
itab-field2 = 'BBB'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.
itab-field2 = 'DDD'.
itab-field1 = 'CC'.
APPEND itab.
CLEAR itab.
LOOP AT itab.
AT NEW field1.
ADD 1 TO counter.
ENDAT.
ENDLOOP.
BREAK-POINT.
Field2 and Field1 have been interchanged.
10-21-2008 6:04 AM
Hi M N
It is working Check this code
TYPES : BEGIN OF TY_SFLIGHT,
CARRID TYPE SFLIGHT-CARRID,
CONNID TYPE SFLIGHT-CONNID,
END OF TY_SFLIGHT.
DATA :IT_SFLIGHT TYPE TABLE OF TY_SFLIGHT,
WA_SFLIGHT TYPE TY_SFLIGHT,
COUNT TYPE I.
SELECT CARRID
CONNID
FROM SFLIGHT
INTO TABLE IT_SFLIGHT.
SORT IT_SFLIGHT BY CARRID CONNID.
LOOP AT IT_SFLIGHT INTO WA_SFLIGHT.
AT NEW CONNID.
COUNT = COUNT + 1.
WRITE : / WA_SFLIGHT-CARRID,
WA_SFLIGHT-CONNID,
COUNT.
ENDAT.
ENDLOOP.
regards
10-21-2008 6:12 AM
Hi MN,
There might be some problem with your table declaration.
Check for the below given code.
The functionality is fine.
data: begin of itab1 occurs 0,
po type i,
item type i,
transaction_event type i,
document type i,
end of itab1.
data wa1 like itab1.
data COUNTER type i.
itab1-po = 100000.
itab1-item = 1.
itab1-transaction_event = 1.
itab1-document = 560000001.
append itab1.
itab1-po = 100000.
itab1-item = 1.
itab1-transaction_event = 1.
itab1-document = 560000002.
append itab1.
itab1-po = 100000.
itab1-item = 1.
itab1-transaction_event = 2.
itab1-document = 620000004.
append itab1.
itab1-po = 100000.
itab1-item = 1.
itab1-transaction_event = 2.
itab1-document = 620000006.
append itab1.
itab1-po = 100000.
itab1-item = 1.
itab1-transaction_event = 2.
itab1-document = 620000007.
append itab1.
loop at itab1.
write:/ itab1-po,
itab1-item,
itab1-transaction_event,
itab1-document.
endloop.
LOOP AT itab1.
AT NEW transaction_event.
ADD 1 TO COUNTER.
ENDAT.
ENDLOOP.
write:/ 'Counter'.
write:/ Counter.
Here I am getting the Counter as 2 only.
Check with the declaration part.
Regards,
Amit.
10-21-2008 6:22 AM
Hi MN,
Do like this,
LOOP AT itab.
on change of Field2.
ADD 1 TO COUNTER.
endon.
ENDLOOP.
Kiran
10-21-2008 7:04 AM
>
> Hi MN,
> Do like this,
>
> LOOP AT itab.
> on change of Field2.
> ADD 1 TO COUNTER.
> endon.
> ENDLOOP.
>
> Kiran
ON CHANGE is obsolete statement. try to avoid that.
10-21-2008 6:30 AM
Hi
Try this..
LOOP AT itab.
ON CHANGE OF itab-f2.
count = count + 1.
ENDON.
ENDLOOP.