11-10-2005 7:25 PM
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
11-10-2005 7:33 PM
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
11-10-2005 7:30 PM
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.
11-10-2005 7:31 PM
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
11-10-2005 7:33 PM
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
11-10-2005 7:33 PM
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
11-10-2005 7:34 PM
Rich is lot quicker than me on the keyboard!! But I am glad that we are thinking on the same lines(.!.)
Srinivas
11-10-2005 7:34 PM
11-10-2005 7:35 PM
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,
11-10-2005 7:40 PM
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.
11-10-2005 7:46 PM
11-10-2005 7:53 PM
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
11-10-2005 11:36 PM
... 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.
11-14-2005 7:54 PM
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..
11-14-2005 8:19 PM
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
11-14-2005 8:38 PM
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.