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

moving internal table data on some condition

Former Member
0 Likes
17,140

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.

9 REPLIES 9
Read only

Former Member
0 Likes
6,692
LOOP AT ITABLE1 WHERE FIELD1 = 'WHAT_YOU_WANT'.
MOVE ITABLE1 TO ITABLE2.
APPEND ITABLE2.
ENDLOOP.
Read only

0 Likes
6,692

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.

Read only

0 Likes
6,692

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.

Read only

0 Likes
6,692

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.

Read only

0 Likes
6,692

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
6,692

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

Read only

0 Likes
6,692

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.

Read only

Former Member
0 Likes
6,692

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

Read only

Former Member
0 Likes
6,692

Answered.