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: 

Loop over a table from end to beginning?

daniel_humberg
Contributor
0 Kudos

I want to loop over a STANDARD TABLE in the opposite direction, i.e. starting with the last entry up to the first entry.

What'S the best way to do this?

Do I have to make a copy of the table first and reverse the sorting?

Can I use LOOP in some way?

Or shall I do something like

DO n TIMES

READ TABLE t INDEX i.

ENDDO.

What if I have a HASHED TABLE (no read with index) ?

Message was edited by: Daniel Humberg

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Here is one way.


DESCRIBE TABLE itab LINES v_numlines.

DO v_numlines TIMES.
  CLEAR itab.
  READ TABLE itab index v_numlines.
  ----- do whatever---
  v_numlines = v_numlines - 1.
ENDDO.

Srinivas

14 REPLIES 14

Former Member
0 Kudos

Hi,

You have to copy the table & sort in descending & then loop the table.

One more thing is you cannot loop at a standard table.

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can do something like this. LOOP does not provide this functionality.



report zrich_0001 .

data: begin of itab occurs 0,
      field1 type c,
      field2 type c,
      end of itab.

data: tablines type i.
data: lineindex type i.

itab-field1 = 'A'.
itab-field2 = '1'.
append itab.

itab-field1 = 'B'.
itab-field2 = '2'.
append itab.

itab-field1 = 'C'.
itab-field2 = '3'.
append itab.


describe table itab lines tablines.

lineindex = tablines.
do tablines times.

  read table itab index lineindex.
  write:/ itab-field1, itab-field2.
  lineindex = lineindex - 1.

enddo.

Regards,

Rich Heilman

0 Kudos

Also, if when building the itab, you assign an index then, then you can sort decending and a loop at it.



report zrich_0001 .

data: begin of itab occurs 0,
      index type i,
      field1 type c,
      field2 type c,
      end of itab.

data: tablines type i.
data: lineindex type i.

itab-index = '1'.
itab-field1 = 'A'.
itab-field2 = '1'.
append itab.

itab-index = '2'.
itab-field1 = 'B'.
itab-field2 = '2'.
append itab.

itab-index = '3'.
itab-field1 = 'C'.
itab-field2 = '3'.
append itab.


sort itab descending by index.

loop at itab.
  write:/ itab-field1, itab-field2.
endloop.

Regards,

Rich HEilman

Former Member
0 Kudos

Here is one way.


DESCRIBE TABLE itab LINES v_numlines.

DO v_numlines TIMES.
  CLEAR itab.
  READ TABLE itab index v_numlines.
  ----- do whatever---
  v_numlines = v_numlines - 1.
ENDDO.

Srinivas

0 Kudos

Rich is lot quicker than me on the keyboard!! But I am glad that we are thinking on the same lines(.!.)

Srinivas

Former Member
0 Kudos

You cannot loop at the Hashed table too.

Washington
Explorer
0 Kudos

Hi Daniel,

it seems strange, but i think u can do this:

describe table t lines <lines>.

while not <lines> = 0.

read table t index <lines>.

<lines> = <lines> - 1.

endwhile.

Regards,

Former Member
0 Kudos

Hi,

You can select entries from Standard table into your internal table by using DESCENDING (Last entry first)by time(Field).

SELECT * FROM SE903 INTO

X_TEMP

WHERE VBELN <> ' '

AND POSNR <> ' '

ORDER BY ERDAT DESCENDING.

0 Kudos

I think we are talking about internal tables here....

Srinivas, Great Minds Think Alike!!!!!

Regards,

Rich HEilman

Former Member
0 Kudos

Daniel,

If you can get the required state by sorting in reverse then it will be the better approach. Otherwise use your second option Do...Enddo with Read.

I don't think there is any other way to loop the internal table in opposite direction.

Thanks

Giridhar

0 Kudos

... do this:

data:

lv_tfill type sytfill.

field-symbols:

<fs> type any.

describe table t lines lv_tfill.

while lv_tfill > 0.

read table t assigning <fs>

index lv_tfill.

write: / <fs>.

subtract 1 from lv_tfill.

endwhile.

A forward loop won't be faster.

C.

daniel_humberg
Contributor
0 Kudos

ok, thanks for all the answers.

Unfortunately, my table is a HASHED table, so I can't READ the table by INDEX. I can'T use SORT either, because the table not sorted by a certain field.

I am thinking of doing it like this


DATA t_copy LIKE STANARD TABLE OF t.
LOOP AT t ASSIGNING <fs>.
  INSERT <fs> into t_copy INDEX 1.
ENDLOOP.

Although it seems not very elegant..

0 Kudos

How about just moving your hashed table to a standard table and then reading backwards.




report zrich_0001 line-size 200 .

<b>types: begin of ttab,
       field1 type c,
       field2 type c,
       end of ttab.

data: itab type hashed table of ttab
              with unique key field1
              with header line.
data: wa like line of itab.

data: itab2 type standard table of ttab with header line.</b>

data: tablines type i.
data: lineindex type i.

start-of-selection.

  wa-field1 = 'A'.
  wa-field2 = '1'.
  insert wa into table itab.

  wa-field1 = 'B'.
  wa-field2 = '2'.
  insert wa into table itab.

  wa-field1 = 'C'.
  wa-field2 = '3'.
  insert wa into table itab.

<b>  itab2[] = itab[].</b>

  describe table itab2 lines tablines.

  lineindex = tablines.

  do tablines times.
    read table itab2 index lineindex.
    write:/ itab2-field1, itab2-field2.
    lineindex = lineindex - 1.
  enddo.

Regards,

Rich Heilman

0 Kudos

yes, that'S what I do.

I just dont do a copy with itab2 = itab1, beacuse i need to sort the table in the same loop that I use for copying.