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

internal table operations

Former Member
0 Likes
820

Hi all,

Please tell me the alternative of using loop that is specified in Bold. some body saying that is an alternative of using some statement without using loop in loop.

* data declaration
DATA: BEGIN OF T1 OCCURS 0,
F1 TYPE I,
END OF T1.

DATA: BEGIN OF T2 OCCURS 0,
F1 TYPE I,
F2(10) TYPE C,
END OF T2.

DATA: T3 LIKE T2 OCCURS 0 WITH HEADER LINE.


* fill internal table T1
T1-F1 =  10.
APPEND T1.

T1-F1 =  20.
APPEND T1.

T1-F1 =  30.
APPEND T1.

T1-F1 =  40.
APPEND T1.

* fill internal table T2
T2-F1 =  10.
T2-F2 = 'AA'.
APPEND T2.

T2-F1 =  10.
T2-F2 = 'AB'.
APPEND T2.

T2-F1 =  10.
T2-F2 = 'AC'.
APPEND T2.

T2-F1 =  20.
T2-F2 = 'BA'.
APPEND T2.

T2-F1 =  20.
T2-F2 = 'BB'.
APPEND T2.

T2-F1 =  30.
T2-F2 = 'CA'.
APPEND T2.

T2-F1 =  30.
T2-F2 = 'CB'.
APPEND T2.

T2-F1 =  30.
T2-F2 = 'CC'.
APPEND T2.

T2-F1 =  40.
T2-F2 = 'DA'.
APPEND T2.

CLEAR: T1, T2.

* fill internal table T1
LOOP AT T1.
<b>  LOOP AT T2 WHERE F1 = T1-F1.
    APPEND T2 TO T3.
  ENDLOOP.</b>
ENDLOOP.

* display internal table T1
LOOP AT T3.
  WRITE:/ T3-F1, T3-F2.
ENDLOOP.

9 REPLIES 9
Read only

Former Member
0 Likes
770

You can do something like this :

LOOP AT T1.

read table t2 with key f1 = t1-fi binary search.

if sy-subrc eq 0.

APPEND T2 TO T3.

endif.

ENDLOOP.

Read only

Former Member
0 Likes
770

Hi,

You could use Read table with key instead of loop within loop.

Rgds,

HR

Read only

Former Member
0 Likes
770

Hi

You can use

  • fill internal table T1

LOOP AT T2.

move-corresponding T2 to T3.

collect T3.

ENDLOOP.

Write T3 contents. (All records of T2 are collected (<b>summated</b>) based on the value F2)

Regards,

Raj

Read only

Former Member
0 Likes
770

Hi,

Since you are populating the value for T1 and T2 manually, why not by populate all the value into one single internal table?

Read only

Former Member
0 Likes
770

Hi,

use below code which is best performance wise

sort t2 by f1.

data lv_index like sy-tabix.

LOOP AT T1.

clear lc_index.

read table t2 with key f1 = t1-f1 binary search.

if sy-subrc eq 0.

lv_index = sy-tabix.

LOOP AT T2 from lv_index.

if t1-f1 <> t2-f1.

exit.

endif.

APPEND T2 TO T3.

ENDLOOP.

endif.

ENDLOOP.

Regards

Amole

Regards

amole

Read only

Former Member
0 Likes
770

HI

I guess for this case, there is no alternative statements as you have data in two internal tables. But

YES you can minimise during extraction via FOR ALL ENTRIES.

Eg:

tables: mara.
select-options: s_matnr for mara-matnr.
types: begin of t_mara,
         matnr like mara-matnr,
         mtart like mara-mtart,
       end of t_mara.
types: begin of t_marc,
         matnr like marc-matnr,
         werks like marc-werks,
       end of t_marc.
data: it_mara type standard table of t_mara,
      it_marc type standard table of t_marc.

select matnr mtart into table it_mara
       from mara
       where matnr in s_matnr.

select matnr werks into table it_marc
       from marc
       for all entries in it_mara
       where matnr = it_mara-matnr.

Kind Regards

Eswar

Read only

Former Member
0 Likes
770

hi,

use READ stmt..

SORT T2.

LOOP AT T1.

READ TABLE T2 WHERE F1 = T1-F1 BINARY SEARCH.

APPEND T2 TO T3.

ENDLOOP.

hope this helps,

do reward if it helps,

priya.

Read only

Former Member
0 Likes
770

Hi Praveen,

Since it is Loop inside Loop. There is no point in having :

Loop T1.

Read T2 with key F1 = T1-f1.

append T2 to T3.

Endloop.

The above logic will add only one(Same )record from T2 to T3 though there could be more than 1 record where T2-F1 = T1-F1.

<b>The current logic which you have written is correct and its the best way to do it.</b>

<b>I would suggest not to look out for any other alternative except SORTING.

SORT T1 By F1.

Sort T2 by F2.</b>

Best regards,

Prashant

PS : Please reward all helpful answers

Read only

Former Member
0 Likes
770

Praveen,

you can do the following with out usign loop in loop.

t3[] = t2[].

loop at t3.

v_index = sy-tabix.

read table t1 with key f1 = t3-f1.

if sy-subrc <> 0.

delete t3 index v_index.

endif.

endloop.

uline.

  • display internal table T1

LOOP AT T3.

WRITE:/ T3-F1, T3-F2.

ENDLOOP.

-Anu