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

Need Internal table logic

Former Member
0 Likes
1,343

Hi,

I have 2 columns in a internal table as below  ..can anyone suggest a logic which gives good performance as the entries in the internal table are huge.

Col1     Col2

22          XYZ

22          XYZ

22          XYZ

23          YTR

23          YTR

24          ABC

25          SDG

25          SDG

I want to run the above IT in a loop and re-arrange the above columns as below ..

Col1     Col2

22          XYZ

22         

22         

23          YTR

23         

24          ABC

25          SDG

25         

The first occurrence in column 1 should have the text in Column 2 and remaining rows should be cleared if the rows match.

Regrads

Praneeth

1 ACCEPTED SOLUTION
Read only

guilherme_frisoni
Contributor
0 Likes
1,304

Hi Praneeth,

you can do the following:

data: l_col2 like it-col2.

sort it by col1 col2.

loop at it assinging <it>.

if l_col2 = <it>-col2.

clear <it>-col2.

else.

l_col2 = <it>-col2.

endif.

endloop.

Regards,

Guilherme Frisoni

11 REPLIES 11
Read only

Former Member
0 Likes
1,304

hello,

if you detele duplicate entries then you can use

Delete adjacent duplicates from it comparing col2...

Thanks

Sabyasachi

Read only

0 Likes
1,304

Hi,

I do not want to delete the row because there are other columns in the IT so cannot use delete adjacent duplicates keyword. I only want to clear the content in column 2.

Regards

Praneeth

Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,304

Hi Praneeth,

For better performance, Use Cursors Logic.

Search for the existing information available.

Search with the keyword "Parallel Cursors"

Read the F1 help on LOOP stmt using FROM and TO additions.

Hope this helps.

Read only

Former Member
0 Likes
1,304

Hi Praneeth,

I NEED SMALL CLARIFICTAION ABOUT UR DATA.

If below data exist than what it will display.

Col1     Col2

22          XYZ

22          XYZ

22          ABC

23          YTR

23          XYX

24          ABC

25          SDG

25          SDG

25          ABC

If Internal Table contains baove data than what will be the OutPut.

Please rpely me than i will give u the Code.

Thanks

Tarak

Read only

0 Likes
1,304

Hi,

Each row is unique . So 25 ABC will not occur as 25 is already related to SDG. It means the value in col 1 is always linked to a unique value in col 2.

Regards

Praneeth

Read only

Former Member
0 Likes
1,304

Hi,

Below code will arrange your table like you asked:

DATA: v_temp TYPE wa-f2.

SORT it BY f1 f2.

LOOP AT it INTO wa.

  IF wa-f2 EQ v_temp.

    CLEAR wa-f2.

    MODIFY it FROM wa.

  ELSE.

    v_temp = wa-f2.

  ENDIF.

ENDLOOP.

Read only

Former Member
0 Likes
1,304

Hello praneeth,                                                        it = it1.

                                                                               wa = wa1.

LOOP AT IT INTO WA.

AT NEW COL2.
MOVE-CORRESPONDING WA TO WA1.
APPEND WA1 TO IT1.
CONTINUE.
ENDAT.
CLEAR WA-COL2.
MOVE-CORRESPONDING WA TO WA1.
APPEND WA1 TO IT1.
ENDLOOP.

Thanks

Sabyasachi

                                                                                          

Read only

guilherme_frisoni
Contributor
0 Likes
1,305

Hi Praneeth,

you can do the following:

data: l_col2 like it-col2.

sort it by col1 col2.

loop at it assinging <it>.

if l_col2 = <it>-col2.

clear <it>-col2.

else.

l_col2 = <it>-col2.

endif.

endloop.

Regards,

Guilherme Frisoni

Read only

0 Likes
1,304

Hi,

For me algorithm from is the simplest and the fastest if you are saying that source table is huge:

- It iterates once through source loop and updates values in place -  just read row and update col2 if needed.

- We need to read all rows at least once to check if col2 from row must be left or it should be cleared. This algorithm reads each row only once.

- It uses assigning so in case of large table is important for performance - data is directly updated.

- It does not need any additional tables/structures, only single l_col2 variable as temporal one.

There is no need to sort also by col2, col1 sorted order is enough.

If values of col2 can be duplicated for different col1 values, like:

col1 = 22     col2 = ABC

col1 = 22     col2 = ABC

col1 = 23     col2 = ABC

... then in the loop if condition must refer to col1 instead of col2:

IF l_col1 = <it>-col1.

...

Regards,

Adam

Read only

Kartik2
Contributor
0 Likes
1,304

Hi,

You can do the following:

sort <fs_tab> by col1 col2.

loop at <fs_tab> assigning <fs_wa>.

at new col2.

  lv_new = c_x.

endat.

if lv_new ne c_x.

  clear <fs_wa>-col2.

endif.

at end of col2.

  clear lv_new.

endat.

endloop.

Regars,
Kartik

Read only

jrg_wulf
Active Contributor
0 Likes
1,304

Hi Praneth,

here's my coding for your problem. But make certain, that your internal table is of type standard table (not hashed)

FIELD-SYMBOLS: <fs_ls>, <fs_lp>, <fs_c1s>, <fs_c1p>, <fs_c2s>.
DATA: l_ix TYPE i VALUE 1.

...



  READ TABLE it INDEX l_ix ASSIGNING <fs_lp>.
  ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_lp> TO <fs_c1p>.
  ADD 1 TO l_ix.
  READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
  ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
  ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
  DO.
    WHILE <fs_c1s> EQ <fs_c1p>.
      CLEAR <fs_c2s>.
      ADD 1 TO l_ix.
      READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.
      ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
      ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
    ENDWHILE.
    READ TABLE it INDEX l_ix ASSIGNING <fs_lp>.
    IF sy-subrc NE 0.
      EXIT.
    ENDIF.
    ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_lp> TO <fs_c1p>.
    ADD 1 TO l_ix.
    READ TABLE it INDEX l_ix ASSIGNING <fs_ls>.
    ASSIGN COMPONENT 'COL1' OF STRUCTURE <fs_ls> TO <fs_c1s>.
    ASSIGN COMPONENT 'COL2' OF STRUCTURE <fs_ls> TO <fs_c2s>.
  ENDDO.