简体   繁体   English

将 FOR 与动态内部表一起使用

[英]Using FOR with a dynamic internal table

I would like to convert the method below to a nested FOR instead of a nested LOOP, but I don't know how to do it since the inner table is dynamic (it can be one of 5 different types).我想将下面的方法转换为嵌套的 FOR 而不是嵌套的 LOOP,但我不知道该怎么做,因为内部表是动态的(它可以是 5 种不同类型之一)。

TYPES: BEGIN OF ty_result,
         lgart TYPE string,
         betrg TYPE string,
         betpe TYPE string,
       END OF ty_result,
       ty_results TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY.
DATA: known_table       TYPE ty_results,
      also_known_table  TYPE ty_results,
      mt_period_results TYPE ty_results.

FIELD-SYMBOLS: <dynamic_table> TYPE STANDARD TABLE,
               <betrg>, <betpe>, <lgart>.

LOOP AT known_table REFERENCE INTO DATA(known_line).

  READ TABLE <dynamic_table> TRANSPORTING NO FIELDS WITH KEY ('LGART') = known_line->*-lgart.

  IF sy-subrc <> 0. CONTINUE. ENDIF.

  DATA(lv_tabix) = sy-tabix.

  LOOP AT <dynamic_table> ASSIGNING FIELD-SYMBOL(<dynamic_line>) FROM lv_tabix.

    UNASSIGN: <betrg>, <betpe>, <lgart>.

    ASSIGN COMPONENT: 'BETPE' OF STRUCTURE <dynamic_line> TO <betpe>,
                      'BETRG' OF STRUCTURE <dynamic_line> TO <betrg>,
                      'LGART' OF STRUCTURE <dynamic_line> TO <lgart>.


    IF <lgart> <> known_line->*-lgart.
      EXIT.
    ENDIF.

    APPEND VALUE ty_result( lgart = <lgart>
                            betrg = <betrg>
                            betpe = <betpe> ) TO mt_period_results.

  ENDLOOP.
ENDLOOP.

When the inner table is not dynamic, I can do it like this:当内表不是动态的时,我可以这样做:

 append lines of value zwta_t_results(
   for known_line in known_table
    for also_known_line  in also_known_table 
      where ( lgart = known_line-lgart )
        ( lgart = known_line-lgart
          betrg = also_known_line-betrg
          betpe = also_known_line-betpe ) to mt_period_results.

So the question is: is it possible to use FOR iterator (as the second method) with a dynamic table?所以问题是:是否可以将 FOR 迭代器(作为第二种方法)与动态表一起使用?

My answer was checked for ABAP 7.52.我的答案是针对 ABAP 7.52 检查的。 Unfortunately, it's currently only possible to use a subset of the static variant of ASSIGN by using LET <fs> = writable_expression IN inside a construction expression (including " FOR " table iterations ), where the "writable expression" is limited to a table expression, NEW and CAST.不幸的是,目前只能通过在构造表达式(包括“ FOR表迭代)中使用LET <fs> = writable_expression IN来使用ASSIGN的 static 变体的子集,其中“可写表达式”仅限于表表达式,新的和演员。 So it's rather limited, there are no equivalences for the dynamic variants of ASSIGN , so you can use only workarounds.所以它相当有限, ASSIGN的动态变体没有等价物,因此您只能使用变通方法。

The syntax after WHERE allows a dynamic expression, so it will be possible to enter WHERE ('LGART = KNOWN_LINE-LGART') . WHERE之后的语法允许使用动态表达式,因此可以输入WHERE ('LGART = KNOWN_LINE-LGART') However, it could be very counter-performing if the loop is nested inside another loop (as it is in your case), so an index should be defined so that to accelerate the iteration.但是,如果循环嵌套在另一个循环中(就像您的情况一样),它可能会非常适得其反,因此应该定义一个索引以加速迭代。 If a secondary index is to be used, then the condition should be USING KEY ('KEYNAME') WHERE ('LGART = KNOWN_LINE-LGART') .如果要使用二级索引,则条件应为USING KEY ('KEYNAME') WHERE ('LGART = KNOWN_LINE-LGART')

Now, here is a workaround for your particular case: you define statically the names of the components, so one possibility is to define a static structure with those component names and use the CORRESPONDING construction operator.现在,这是针对您的特定情况的解决方法:您静态定义组件的名称,因此一种可能性是使用这些组件名称定义 static 结构并使用CORRESPONDING构造运算符。 Note that I didn't test it, but I think for several reasons that the performance of using CORRESPONDING is faster in your case than using ASSIGN .请注意,我没有对其进行测试,但我认为出于多种原因,在您的情况下使用CORRESPONDING的性能比使用ASSIGN更快。

The following code should work.以下代码应该可以工作。 I assume that the internal table behind <dynamic_table> has a primary key sorted by LGART ( TYPE SORTED TABLE OF... WITH NON-UNIQUE KEY lgart ) so that the performance is good:我假设<dynamic_table>后面的内部表有一个按LGART排序的主键( TYPE SORTED TABLE OF... WITH NON-UNIQUE KEY lgart ),所以性能很好:

TYPES: BEGIN OF ty_struc,
         lgart TYPE string,
         betrg TYPE string,
         betpe TYPE string,
       END OF ty_struc.

known_table = VALUE #( ( lgart = 'A' ) ( lgart = 'B' ) ).
also_known_table = VALUE #( ( lgart = 'A' ) ( lgart = 'C' ) ( lgart = 'A' ) ).
ASSIGN also_known_table TO <dynamic_table>.

APPEND LINES OF
    VALUE ty_results(
      FOR known_line IN known_table
        FOR <inner_line> IN <dynamic_table>
          WHERE ('LGART = KNOWN_LINE-LGART')
          LET struc = CORRESPONDING ty_struc( <inner_line> ) IN
          ( lgart = known_line-lgart
            betrg = struc-betrg
            betpe = struc-betpe ) )
    TO mt_period_results.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM