2008 Aug 22 10:20 AM
Hi,
i have faced same problem , for at least two occasions, and 'm applying a solution, but i'll like to know if there is a better one (for performance reasons, or other ones , memory , etc...)
The problem is that i need to loop an internal table (a large one), searching for some key entries, and for each one , search for all entries in same table with same value in other field (and do some logic for each one ).
The logic i am applying is to copy the internal table, in an auxiliary internal table, and sort the original table with first criteria and the second with the other criteria.
The two tables are standard.
Then i have 2 nested loops , for searching all the entries with some keys , and for each one loop for all related entries with same value in another field (second key). In both cases i read using binary search, and then loop from that index , until key values change.
For example, here is a code extract:
FORM filtra_punteros_exc TABLES pt_pit_pointer STRUCTURE bdcp
DATA: lt_pit_pointer_sort_mat TYPE TABLE OF bdcp,
wa_pit_pointer LIKE LINE OF pt_pit_pointer,
wa_pit_pointer_sort LIKE LINE OF lt_pit_pointer_sort_mat.
...
sort original table
SORT pt_pit_pointer BY cdobjcl tabname fldname.
auxiliary table, sorted by other criteria
lt_pit_pointer_sort_mat[] = pt_pit_pointer[].
SORT lt_pit_pointer_sort_mat BY cdobjid.
loop all keys
LOOP AT pt_point_exc INTO wa_point_exc.
read key
READ TABLE pt_pit_pointer WITH KEY
cdobjcl = wa_point_exc-cdobjcl
tabname = wa_point_exc-tabname
fldname = wa_point_exc-fldname
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix = sy-tabix.
read all entries with this key
LOOP AT pt_pit_pointer INTO wa_pit_pointer FROM l_tabix.
Leave the loop, if last relevant entry one read
IF wa_pit_pointer-cdobjcl <> wa_point_exc-cdobjcl OR
wa_pit_pointer-tabname <> wa_point_exc-tabname OR
wa_pit_pointer-fldname <> wa_point_exc-fldname.
EXIT.
ENDIF.
for each key, search for all entries with same value in cdobjid
READ TABLE lt_pit_pointer_sort_mat WITH KEY
cdobjid = wa_pit_pointer-cdobjid
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix2 = sy-tabix.
LOOP AT lt_pit_pointer_sort_mat INTO wa_pit_pointer_sort FROM l_tabix2.
Leave the loop, if last relevant entry one read
IF wa_pit_pointer_sort-cdobjid <> wa_pit_pointer-cdobjid.
EXIT.
ENDIF.
some logic here ...
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
Thanks in advance for your valuable sugestions,
Miquel
Hi,
i have faced same problem , for at least two occasions, and 'm applying a solution, but i'll like to know if there is a better one (for performance reasons, or other ones , memory , etc...)
The problem is that i need to loop an internal table (a large one), searching for some key entries, and for each one , search for all entries in same table with same value in other field (and do some logic for each one ).
The logic i am applying is to copy the internal table, in an auxiliary internal table, and sort the original table with first criteria and the second with the other criteria.
The two tables are standard.
Then i have 2 nested loops , for searching all the entries with some keys , and for each one loop for all related entries with same value in another field (second key). In both cases i read using binary search, and then loop from that index , until key values change.
For example, here is a code extract:
FORM filtra_punteros_exc TABLES pt_pit_pointer STRUCTURE bdcp
DATA: lt_pit_pointer_sort_mat TYPE TABLE OF bdcp,
wa_pit_pointer LIKE LINE OF pt_pit_pointer,
wa_pit_pointer_sort LIKE LINE OF lt_pit_pointer_sort_mat.
...
sort original table
SORT pt_pit_pointer BY cdobjcl tabname fldname.
auxiliary table, sorted by other criteria
lt_pit_pointer_sort_mat[] = pt_pit_pointer[].
SORT lt_pit_pointer_sort_mat BY cdobjid.
loop all keys
LOOP AT pt_point_exc INTO wa_point_exc.
read key
READ TABLE pt_pit_pointer WITH KEY
cdobjcl = wa_point_exc-cdobjcl
tabname = wa_point_exc-tabname
fldname = wa_point_exc-fldname
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix = sy-tabix.
read all entries with this key
LOOP AT pt_pit_pointer INTO wa_pit_pointer FROM l_tabix.
Leave the loop, if last relevant entry one read
IF wa_pit_pointer-cdobjcl <> wa_point_exc-cdobjcl OR
wa_pit_pointer-tabname <> wa_point_exc-tabname OR
wa_pit_pointer-fldname <> wa_point_exc-fldname.
EXIT.
ENDIF.
for each key, search for all entries with same value in cdobjid
READ TABLE lt_pit_pointer_sort_mat WITH KEY
cdobjid = wa_pit_pointer-cdobjid
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix2 = sy-tabix.
LOOP AT lt_pit_pointer_sort_mat INTO wa_pit_pointer_sort FROM l_tabix2.
Leave the loop, if last relevant entry one read
IF wa_pit_pointer_sort-cdobjid <> wa_pit_pointer-cdobjid.
EXIT.
ENDIF.
some logic here ...
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
Thanks in advance for your valuable sugestions,
Miquel
2008 Aug 22 12:13 PM
Sorry for bad formatting. I try to arrange it:
FORM filtra_punteros_exc TABLES pt_pit_pointer STRUCTURE bdcp
DATA: lt_pit_pointer_sort_mat TYPE TABLE OF bdcp,
wa_pit_pointer LIKE LINE OF pt_pit_pointer,
wa_pit_pointer_sort LIKE LINE OF lt_pit_pointer_sort_mat.
...
*sort original table
SORT pt_pit_pointer BY cdobjcl tabname fldname.
*auxiliary table, sorted by other criteria
lt_pit_pointer_sort_mat] = pt_pit_pointer[.
SORT lt_pit_pointer_sort_mat BY cdobjid.
*loop all keys
LOOP AT pt_point_exc INTO wa_point_exc.
*read key
READ TABLE pt_pit_pointer WITH KEY
cdobjcl = wa_point_exc-cdobjcl
tabname = wa_point_exc-tabname
fldname = wa_point_exc-fldname
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix = sy-tabix.
* read all entries with this key
LOOP AT pt_pit_pointer INTO wa_pit_pointer FROM l_tabix.
* Leave the loop, if last relevant entry one read
IF wa_pit_pointer-cdobjcl NE wa_point_exc-cdobjcl OR
wa_pit_pointer-tabname NE wa_point_exc-tabname OR
wa_pit_pointer-fldnamem NE wa_point_exc-fldname.
EXIT.
ENDIF.
* for each key, search for all entries with same value in
* cdobjid
READ TABLE lt_pit_pointer_sort_mat WITH KEY
cdobjid = wa_pit_pointer-cdobjid
BINARY SEARCH
TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
l_tabix2 = sy-tabix.
LOOP AT lt_pit_pointer_sort_mat INTO wa_pit_pointer_sort FROM l_tabix2.
* Leave the loop, if last relevant entry one read
IF wa_pit_pointer_sort-cdobjid NE wa_pit_pointer-cdobjid.
EXIT.
ENDIF.
* some logic here ...
ENDLOOP.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
| User | Count |
|---|---|
| 6 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |