‎2009 Mar 05 6:52 PM
Hi All,
I wish to index and display my internal table. How do i do this? for example in my Internal Table I have the following data:-
Material-----Plant--
ABCD1--
1
ABCD1--
2
ABCD1--
3
ABCD2--
1
ABCD2--
2
Basically based on the unique values in Material and Plant an Index is generated? does that make sense?
Thanks
Adeel
‎2009 Mar 05 7:18 PM
Hi Adeel,
Please test the following Sample Code hope will solve out your problem,
DATA: BEGIN OF it_m OCCURS 10,
index TYPE i,
material(6),
amount TYPE i,
END OF it_m.
DATA: it_m2 LIKE STANDARD TABLE OF it_m WITH HEADER LINE.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.
SORT it_m BY material.
DATA: index TYPE i.
LOOP AT it_m INTO it_m.
AT NEW material.
ADD: 1 TO index.
ENDAT.
MOVE-CORRESPONDING it_m TO it_m2.
it_m2-index = index.
APPEND it_m2.
ENDLOOP.Please Reply if any issue,
Kind Regards,
Faisal
‎2009 Mar 05 7:19 PM
hi,
Use the following loop.
data w_index type i.
loop at itab into wa.
at new material.
w_index = 1.
endat.
write : wa-material, wa-plant, w_index.
" if you want to modify internal table then you can use the code below added to the above code...
" wa-index = w_index.
" modify itab from wa index sy-tabix.
endloop.Regards,
Siddarth
‎2009 Mar 05 7:25 PM
Hi, Adeel
Test the following to i think this time it is according to your requirement now.
DATA: BEGIN OF it_m OCCURS 10,
material(6),
amount TYPE i,
index TYPE i,
END OF it_m.
DATA: it_m2 LIKE STANDARD TABLE OF it_m WITH HEADER LINE.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.
SORT it_m BY material.
DATA: index TYPE i.
LOOP AT it_m INTO it_m.
AT NEW material.
ADD: 1 TO index.
ENDAT.
MOVE-CORRESPONDING it_m TO it_m2.
it_m2-index = index.
APPEND it_m2.
ADD: 1 TO index.
AT END OF material.
CLEAR: index.
ENDAT.
ENDLOOP.
LOOP AT it_m2.
WRITE: / it_m2-material, it_m2-amount, it_m2-index.
ENDLOOP.Please Reply if any Issue,
Kind Regards,
Faisal
‎2009 Mar 05 8:14 PM
Siddarth's suggestion would work, except that he forgot to increment the variable.
data w_index type i.
loop at itab into wa.
at new material.
w_index = 1.
endat.
add 1 to w_index.
write : wa-material, wa-plant, w_index.
" if you want to modify internal table then you can use the code below added to the above code...
" wa-index = w_index.
" modify itab from wa index sy-tabix.
endloop.
___
Sudhi Karkada
<a href="http://main.nationalmssociety.org/site/TR/Bike/TXHBikeEvents?px=5888378&pg=personal&fr_id=10222">Biking for MS Relief</a>
‎2009 Mar 05 8:30 PM
Hi,
I think he also forget to clear INDEX in So Both Code will not work fine for the condition, Please Have a look at the code bellow it will work accordingly i have tested,
DATA: index TYPE i.
LOOP AT it_m INTO it_m.
AT NEW material.
CLEAR: index.
ADD: 1 TO index.
ENDAT.
it_m-index = index.
WRITE: / it_m-material, it_m-amount, it_m-index.
ADD: 1 TO index.
ENDLOOP.Kind Regards,
Faisal
‎2009 Mar 05 11:32 PM
Hi Guys,
Tried them it doesn't seem to work. I might try a different approach. i have the following loop.
What i am trying to do is bring the last price of an entry from itab and storing this in itab. Example
*Material----
Price----
Previous Price*
ABC1 -
-
£10----
-
£0 (as this is the first record)
ABC1 -
-
£30----
-
£10
ABC2 -
-
£15----
£30 *<----
-
ERROR*
ABC2 -
-
£45----
-
£15
So basically it is working exactly how i want it apart from when there is a new Material it isn't clearing the previous price even though there is no previous price....does that make sense? I was thinking of doing something via counting etc... but you guys might know an easier solution. Code is below how can i clear the previous price using the example above.
Please note this code does exactly what i want apart from clearing the previous price if it doesn;t exist.
LOOP AT itab.
MOVE sy-tabix TO chnge. " sy-tabix counts no of records.
IF sy-tabix > 1.
w_num = sy-tabix.
w_num = w_num - 1.
MOVE konp-kbetr TO itab-price.
ENDIF.
ENDLOOP.
‎2009 Mar 05 11:40 PM
Hi,
use the logic mentioned below
data : prev_price like itab-price,
mat like itab-price.
loop at itab.
if mat <> itab-material.
mat = itab-material.
clear prev_price.
endif.
itab-previous_price = prev_price.
prev_price = itab-price.
modify itab.
endloop.
‎2009 Mar 06 8:10 AM
Hi, Adeel
Test the code bellow it working accordingly hope this time will solve out your problem i have tested it is working fine as per you requirement.
DATA: BEGIN OF it_m OCCURS 10,
material(6),
amount TYPE i,
p_amount TYPE i,
END OF it_m.
DATA: wa_it_m LIKE it_m,
c_record LIKE sy-tabix,
p_record LIKE sy-tabix.
it_m-material = 'AAAAA'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 20.
APPEND it_m TO it_m.
it_m-material = 'AAAAA'.
it_m-amount = 30.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 10.
APPEND it_m TO it_m.
it_m-material = 'BBBBB'.
it_m-amount = 20.
APPEND it_m TO it_m.
DATA: index TYPE i.
BREAK-POINT.
LOOP AT it_m INTO it_m.
c_record = sy-tabix.
p_record = c_record - 1.
READ TABLE it_m INTO wa_it_m INDEX p_record.
IF sy-subrc EQ 0.
IF it_m-material EQ wa_it_m-material.
it_m-p_amount = wa_it_m-amount.
ELSE.
CLEAR: it_m-p_amount.
ENDIF.
ENDIF.
WRITE: / it_m-material, it_m-amount, it_m-p_amount.
MODIFY it_m FROM it_m INDEX c_record.
ENDLOOP.
Please Reply if any Issue,
Kind Regards,
Faisal
‎2009 Mar 06 5:03 AM
Hi Adeel,
Check the below logic, its working.
Here it_tab has the fields matnr, price, price_prev and wa_tab is the work area for the same.
w_prev stores the previous price
LOOP AT it_tab INTO wa_tab.
w_flag = 'X'. "Set the flag
AT NEW matnr.
CLEAR w_flag. "If its ne material clear it
ENDAT.
IF w_flag EQ 'X'. "If the flag has value X still then that is no the new material
wa_tab-price_prev = w_prev. "So assign the previous value
MODIFY it_tab FROM wa_tab TRANSPORTING price_prev. "And upadate the int tab with prev value
ENDIF.
w_prev = wa_tab-price. "Store the currt price in the so that it'll be used as prev price in next loop
ENDLOOP.Regards,
Manoj Kumar P
Edited by: Manoj Kumar on Mar 6, 2009 6:04 AM
‎2009 Apr 08 1:44 PM
No useful replies. Created an ABAP Program instead of query.
Thread Closed