英文:
How to avoid nested looping over internal tables in ABAP?
问题
你能避免在这两个内部表上使用嵌套循环吗?
DATA: t_fieldcat TYPE slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
LOOP AT T_FIELDCAT INTO LX_FIELDCAT.
LOOP AT T_FCAT INTO LX_FCAT.
***如果它们具有相同的字段名,将FCAT的COL_POS传递给FIELDCAT。***
IF LX_FIELDCAT-FIELDNAME = LX_FCAT-FIELDNAME.
LX_FIELDCAT-COL_POS = LX_FCAT-COL_POS.
APPEND lx_fieldcat TO t_fieldcat.
CLEAR lx_fieldcat.
ENDIF.
ENDLOOP.
ENDLOOP.
我想知道是否有可能在不使用嵌套循环的情况下将内部表T_FCAT
的字段COL_POS
分配给内部表T_FIELDCAT
的字段COL_POS
。
英文:
Can you avoid the nested loop on these two internal tables?
DATA: t_fieldcat TYPE slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
LOOP AT T_FIELDCAT INTO LX_FIELDCAT.
LOOP AT T_FCAT INTO LX_FCAT.
***If they have the same fieldname, the col_pos from FCAT will be passed to FIELDCAT.***
IF LX_FIELDCAT-FIELDNAME = LX_FCAT-FIELDNAME.
LX_FIELDCAT-COL_POS = LX_FCAT-COL_POS.
APPEND lx_fieldcat TO t_fieldcat.
CLEAR lx_fieldcat.
ENDIF.
ENDLOOP.
ENDLOOP.
I want to know if it is possible to assign the field COL_POS
of the internal table T_FCAT
to the field COL_POS
of the internal table T_FIELDCAT
without the nested looping.
答案1
得分: 1
以下是翻译好的部分:
所以坏消息是,我还没有找到不使用两个循环的方法。
但好消息是,我找到了另外两种解决方案。
- 第一种解决方案与您的问题相似,使用了field-symbols和在循环中使用WHERE子句,这样可以删除IF条件,但功能相同。对于VALUE语法,我使用BASE从现有结构(行)复制了所有内容,并使用来自另一个表的值更改其中一个字段。
DATA: t_fieldcat TYPE TABLE OF slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
LOOP AT t_fieldcat ASSIGNING FIELD-SYMBOL(<ls_fieldcat>).
LOOP AT t_fcat ASSIGNING FIELD-SYMBOL(<ls_fcat>) WHERE fieldname = <ls_fieldcat>-fieldname.
<ls_fieldcat> = VALUE #( BASE <ls_fieldcat> ( col_pos = <ls_fcat>-col_pos ) ).
ENDLOOP.
ENDLOOP.
- 第二种解决方案要短得多,但对于没有学习新语法的人来说可能更复杂,但现在是最好的时间!
DATA: t_fieldcat TYPE TABLE OF slis_fieldcat_alv,
t_fieldcat_new TYPE TABLE OF slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
t_fieldcat_new = VALUE #( FOR item IN t_fieldcat
FOR fcat IN t_fcat
WHERE ( fieldname = item-fieldname )
( VALUE #( BASE item col_pos = fcat-col_pos ) ) ).
这会将记录写入新表,使用FOR循环遍历t_fieldcat中的所有记录,以及t_fcat中的所有记录。这就像使用了两个循环。VALUE BASE语法与第一个解决方案中的相同。
这种语法可能当然不适用于较旧的系统。
祝编码愉快!
英文:
So the bad news is that I have not figured out a way to do this without two loops.
But the good news is that I have found out two other solutions for this.
-
The first one being rather similar to your question, using field-symbols and a WHERE clause in the LOOP, which removes the IF condition, but works the same.
For the VALUE syntax, I copy everything from the existing structure(row) using BASE, and change one of the fields using the value from the other table.DATA: t_fieldcat TYPE TABLE OF slis_fieldcat_alv, t_fcat     TYPE kkblo_t_fieldcat. LOOP AT t_fieldcat ASSIGNING FIELD-SYMBOL(<ls_fieldcat>).   LOOP AT t_fcat ASSIGNING FIELD-SYMBOL(<ls_fcat>)  WHERE fieldname = <ls_fieldcat>-fieldname. <ls_fieldcat> = VALUE #( BASE <ls_fieldcat>  ( col_pos = <ls_fcat>-col_pos ) ).         ENDLOOP. ENDLOOP.
-
The second solutions is much shorter, but much more complex for someone who did not learn the new syntax, but when is the best time if not now!
DATA: t_fieldcat TYPE TABLE OF slis_fieldcat_alv, t_fieldcat_new TYPE TABLE OF slis_fieldcat_alv, t_fcat TYPE kkblo_t_fieldcat. t_fieldcat_new = VALUE #( FOR item IN t_fieldcat FOR fcat IN t_fcat WHERE ( fieldname = item-fieldname ) ( VALUE #( BASE item col_pos = fcat-col_pos ) ) ).
This writes the records in a new table, using the FOR loop I move across all the records in t_fieldcat, and the same with the t_fcat. This is like using two LOOPs. And the VALUE BASE syntax is the same like in the first solution.
This syntax might of course not work on the older systems.
Happy coding!
答案2
得分: 0
Use could just use the read table statement.
使用 read table 语句。
read table t_fcat into lx_fcat with key fieldname = lx_fieldcat-fieldname.
if sy-subrc = 0.
...
endif.
使用 read table 从 t_fcat 表中读取数据到 lx_fcat,使用字段名 fieldname 作为关键字。如果 sy-subrc 等于 0,则执行...
But you're not passing the field col_pos to the other fieldcatalog. You're adding a new line to it which will probably run into an endless loop. I would assign a field-symbol in the first loop and write the col_pos into it:
但是你没有将字段 col_pos 传递给其他的字段目录。你只是向它添加了一行新数据,可能会导致无限循环。我建议在第一个循环中分配一个字段符号,并将 col_pos 写入其中。
data: t_fieldcat type table of slis_fieldcat_alv,
t_fcat type kkblo_t_fieldcat,
lx_fcat type kkblo_fieldcat.
field-symbols: <lx_fieldcat> type slis_fieldcat_alv.
数据声明:t_fieldcat 类型为 slis_fieldcat_alv 表,
t_fcat 类型为 kkblo_t_fieldcat,
lx_fcat 类型为 kkblo_fieldcat。
字段符号声明:<lx_fieldcat> 类型为 slis_fieldcat_alv。
loop at t_fieldcat assigning <lx_fieldcat>.
read table t_fcat into lx_fcat with key fieldname = <lx_fieldcat>-fieldname.
if sy-subrc = 0.
If they have the same fieldname, the col_pos from FCAT will be passed to FIELDCAT.
如果它们具有相同的字段名,来自 FCAT 的 col_pos 将传递给 FIELDCAT。
<lx_fieldcat>-col_pos = lx_fcat-col_pos.
endif.
endloop.
英文:
Use could just use the read table statement.
read table t_fcat into lx_fcat with key fieldname = lx_fieldcat-fieldname.
if sy-subrc = 0.
...
endif.
But you're not passing the field col_pos to the other fieldcatalog. You're adding a new line to it which will probably run into an endless loop. I would assign a field-symbol in the first loop and write the col_pos into it:
data: t_fieldcat type table of slis_fieldcat_alv,
t_fcat type kkblo_t_fieldcat,
lx_fcat type kkblo_fieldcat.
field-symbols: <lx_fieldcat> type slis_fieldcat_alv.
loop at t_fieldcat assigning <lx_fieldcat>.
read table t_fcat into lx_fcat with key fieldname = <lx_fieldcat>-fieldname.
if sy-subrc = 0.
***If they have the same fieldname, the col_pos from FCAT will be passed to FIELDCAT.***
<lx_fieldcat>-col_pos = lx_fcat-col_pos.
endif.
endloop.
答案3
得分: 0
为了避免两个嵌套循环,从而将渐近复杂度从O(n2)降低到O(n),我建议定义一个散列表类型,以fieldname作为字段目录表查找的键。它们经过优化,通过主表键进行访问,并获得恒定的访问时间,无论行数多少。
我们还应该记住,散列表需要额外的内存,并且基于分布式内存管理,即数据不按顺序存储在内存中,条目的内存位置是根据唯一键的散列算法计算的。因此,我们实际上是在以内存换取速度。
我们定义类型:
WITH UNIQUE KEY fieldname.
然后仅使用一个循环和一个散列表进行查找:
t_fieldcat_new TYPE TABLE OF slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
...
t_fieldcat_new = VALUE #( FOR fcat IN t_fcat
( LINES OF COND #( WHEN line_exists( t_fieldcat[ fieldname = fcat-fieldname ] )
THEN VALUE #(
LET <fs_fieldcat> = t_fieldcat[ fieldname = fcat-fieldname ] IN
( CORRESPONDING #( BASE ( <fs_fieldcat> ) fcat ) ) )
) ) ).
在这种情况下,语法更加简洁,但它有效。
英文:
To avoid two nested loops and therefore reduce the asymptomatic complexity from O(n<sup>2</sup>) to O(n) I would suggest to define a hashed table type with fieldname as a key for field catalog table lookups. They are optimized for access via the primary table key and attain a constant access time, irrespective of the number of lines.
We should also keep in mind that hash table requires additional memory and are based on distributed memory management, i. e. the data is not stored in order in the memory and the memory position of an entry is calculated using the hash algorithm on the basis of a unique key. So we would actully trade memory for speed.
We define the type:
TYPES: ty_t_fieldcat_hashed TYPE HASHED TABLE OF slis_fieldcat_alv
WITH UNIQUE KEY fieldname.
and use afterwards only one loop and a hash table for a lookup:
DATA: t_fieldcat TYPE ty_t_fieldcat_hashed,
t_fieldcat_new TYPE TABLE OF slis_fieldcat_alv,
t_fcat TYPE kkblo_t_fieldcat.
...
t_fieldcat_new = VALUE #( FOR fcat IN t_fcat
( LINES OF COND #( WHEN line_exists( t_fieldcat[ fieldname = fcat-fieldname ] )
THEN VALUE #(
LET <fs_fieldcat> = t_fieldcat[ fieldname = fcat-fieldname ] IN
( CORRESPONDING #( BASE ( <fs_fieldcat> ) fcat ) ) )
) ) ).
The syntax in this case is even more concise, but it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论