‎2010 Aug 17 5:06 PM
Hi,
I want to move the data from internal table 1 to the internal table 2 on some condition of a field on the first internal table. Please let me know how I can do this.
Regards,
Guru Charan.
‎2010 Aug 17 5:17 PM
LOOP AT ITABLE1 WHERE FIELD1 = 'WHAT_YOU_WANT'.
MOVE ITABLE1 TO ITABLE2.
APPEND ITABLE2.
ENDLOOP.
‎2010 Aug 17 5:27 PM
Cant we write the condition on the move statement?
Shall I have to loop through the entire table for doing so?
Is there any efficient code still?
Regards,
Guru Charan.
‎2010 Aug 17 5:31 PM
No, you can't use the filter at the move statement.
If you want to move the entire data from TABLE1 to TABLE2, both have the same structure, and you want to check only the first field of the first entry you can use:
READ TABLE ITABLE1 INDEX 1.
IF ITABLE1-FIELD1 = 'WHAT_YOU_WANT'.
ITABLE2[] = ITABLE1[].
ENDIF.
‎2010 Aug 17 5:42 PM
Let me make my requirement in simple words:
We have an internal table with three fields-ITEM, SEASON and DEMAND_SEEN.
DEMAND_SEEN takes only two values either 0(demand is seen) or 1(no demand is seen).
if we parse through the internal table, there are several records with the same item but difference in the other two columns.
So, first I have sorted the table by ITEM filed.
Now, while parsing, suppose if I find that the ITEM has 0 under DEMAND_SEEN at any parse, then I need to copy all the records of that item into another internal table-->meaning, I need to copy from the current internal table to the newly created internal table based on the condition of ITEM name.
And suppose if no demand is seen for an item, we just have to neglect it. And please note that if demand is seen for an item, there is no need to parse again for that item in the internal table, but we need to have the copy of all the records related to tht item into the other internal table.
How do I do that?
Regards,
Guru Charan.
‎2010 Aug 17 6:07 PM
Hi,
Try this:
LOOP AT INTERNAL_TABLE1 INTO WORKAREA WHERE DEMAND_SEEN = '0'. " gives all items with demanad_seen = 0
APPEND WORKAREA TO INTERNAL_TABLE2.
ENDLOOP.
Regards
Prasenjit
‎2010 Aug 17 7:09 PM
Hello Guru Charan,
Check this code snippet:
TYPES:
BEGIN OF ty_data,
item TYPE numc3,
season TYPE char10,
demand TYPE char1,
END OF ty_data.
DATA: it_data TYPE STANDARD TABLE OF ty_data,
wa_data TYPE ty_data,
it_meaning TYPE STANDARD TABLE OF ty_data,
it_temp TYPE STANDARD TABLE OF ty_data,
wa_temp TYPE ty_data,
v_index TYPE sy-index VALUE 1.
SORT it_data BY item demand.
it_temp = it_data.
* Keep the items for which demand is seen
DELETE ADJACENT DUPLICATES FROM it_temp COMPARING item demand.
* Classic example where nested loops are unavoidable :-)
LOOP AT it_temp INTO wa_temp.
* Loop through the records for which demand is seen
LOOP AT it_data INTO wa_data FROM v_index.
IF wa_data-item = wa_temp-item.
APPEND wa_data to it_meaning. "Add the record to meaning table
ELSE.
v_index = sy-tabix. "Set the index value & exit !!!
EXIT.
ENDIF.
ENDLOOP.
ENDLOOP.Hope this helps.
BR,
Suhas
‎2010 Aug 17 9:32 PM
Hi Suhas,
Appreciated your enthu in writing the code exactly according to the requirement. And very much thanks for giving out the conclusion on what exactly I needed.........that we definitely have to use two loops in a requirement like this!
Let me however give out my code.......
data:
ITAB type table of SOURCE_PACKAGE,
WA1 like ITAB,
WA2 like ITAB,
ITM type SOURCE_PACKAGE-ITEM.
move SOURCE_PACKAGE[] to ITAB[].
sort ITAB by ITEM.
clear SOURCE_PACKAGE[].
refresh SOURCE_PACKAGE[].
loop at ITAB into WA2.
if WA2-DEMAND ne '0'.
loop at ITAB into WA1 where ITM = WA2-ITEM.
append WA1 to SOURCE_PACKAGE.
endloop.
endif.
endloop.Regards,
Guru Charan.
‎2010 Aug 18 6:55 AM
Hi gurucharan,
why dont you read the internal table on condition instead of looping.
read table itab1 into wa2 with key ( ur condition here).
This will avoid loop inside a loop
regards,
Mithun
‎2010 Sep 01 6:11 PM