Application Development 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: 

internal table

Former Member
0 Kudos
103

hello Gurus,

How to reverse the content of an internal table ?

for example : if my itab contains

a b c d

how can i get d c b a??

thanks in advance..

Cheers,

Sriram.T

1 ACCEPTED SOLUTION

Former Member
0 Kudos
81

sort itab descending.

7 REPLIES 7

Former Member
0 Kudos
82

sort itab descending.

abdulazeez12
Active Contributor
0 Kudos
81

say itab1 has a, b, c, d fields..

declare another internal table itab2 with same field names but in the order d, c, b and a.

loop at itab1.

move-corresponding itab1 to itab2.

append itab2.

endloop.

now itab2 is ready with d c b a.

Cheers

Shakir

Former Member
0 Kudos
81

check this coding

data : begin of itab occurs 0,

f1,

end of itab.

itab-f1 = 'a'.

append itab.

itab-f1 = 'b'.

append itab.

itab-f1 = 'c'.

append itab.

itab-f1 = 'd'.

append itab.

sort itab descending by f1.

loop at itab.

write : / itab-f1.

endloop.

0 Kudos
81

Hi this is Ok ...Suppose if my internal table has the contents of matnr ERSDA and ERNAME fields and now i would like to reverse the itab such that the last row becomes first and the first row becomes last..How can i do that ??

Former Member
0 Kudos
81

then you can try like this

data : begin of itab occurs 0,

f1,

end of itab.

data : lin type i,

count type i.

data : itab1 like itab occurs 0 with header line.

itab-f1 = 'a'.

append itab.

itab-f1 = 'b'.

append itab.

itab-f1 = 'c'.

append itab.

itab-f1 = 'd'.

append itab.

describe table itab lines lin.

count = lin.

do lin times.

if count ne 0.

read table itab into itab1 index count.

append itab1.

endif.

count = count - 1.

enddo.

loop at itab1.

write : / itab1-f1.

endloop.

Former Member
0 Kudos
81

Hi Sriram,

Try as illustrated below.

define one more field as sequence along with other fieldnames as itab1(say - seq, field1, field2).

data : l_seq type i.

loop at itab.

move-corresponding itab to itab1

itab1-seq = l_seq + 1.

append itab1.

endloop.

sort itab1 by seq desencding.

data : itab_final like table of itab.

loop at itab1.

move corresponding itab1 to itab_final.

append itab_final.

endloop.

Regards,

Mohaiyuddin

Former Member
0 Kudos
81

Hi,

There is an easiest way to do this.

tables: mara.

TYPES:BEGIN OF ty_mara,

matnr TYPE mara-matnr,

ersda TYPE mara-ersda,

ernam TYPE mara-ernam,

END OF ty_mara.

DATA: it_mara TYPE TABLE OF ty_mara,

wa_mara TYPE ty_mara,

it_mara_rev TYPE TABLE OF ty_mara,

wa_mara_rev TYPE ty_mara,

l_count(100) TYPE c.

SELECT-OPTIONS: s_matnr FOR mara-matnr.

SELECT matnr

ersda

ernam

FROM mara

INTO CORRESPONDING FIELDS OF TABLE it_mara

WHERE matnr IN s_matnr.

describe table it_mara lines l_count.

CONDENSE l_count.

WHILE l_count GT 0 and l_count NE 0.

IF sy-index = 1.

READ TABLE it_mara INTO wa_mara INDEX l_count.

wa_mara_rev = wa_mara.

APPEND wa_mara_rev TO it_mara_rev.

ELSEIF sy-index GE 1.

l_count = l_count - 1.

condense l_count.

if l_count eq 0.

exit.

endif.

READ TABLE it_mara INTO wa_mara INDEX l_count.

wa_mara_rev = wa_mara.

APPEND wa_mara_rev TO it_mara_rev.

ENDIF.

ENDWHILE.

loop at it_mara into wa_mara.

write:/1 wa_mara-matnr, 20 wa_mara-ersda, 40 wa_mara-ernam.

endloop.

write:/1 sy-uline(100).

loop at it_mara_rev into wa_mara_rev.

write:/1 wa_mara_rev-matnr, 20 wa_mara_rev-ersda, 40 wa_mara_rev-ernam.

endloop.

This should definetely help you,

Reward points if so.

Regards,

Arul.