将值分配给动态结构

时间:2017-12-13 14:18:02

标签: sap abap function-module

需要了解下面的代码,关于如何简化。下面的代码运作良好,但有没有办法可以增强或缩短代码使其动态化?

    TYPES: BEGIN OF lty_dates,
             yesterday TYPE string,
             today     TYPE string,
             tomorrow  TYPE string,
           END OF lty_dates.

    DATA: it_table TYPE TABLE OF lty_dates.

    DO 3 TIMES.
      CASE lv_count.
        WHEN 1.
          it_table[ 1 ]-zyesterday = 'Result Yesterday'.
          it_table[ 2 ]-zyesterday = 'Result Yesterday'.
          it_table[ 3 ]-zyesterday = 'Result Yesterday'.

        WHEN 2.
          it_table[ 1 ]-ztoday = 'Result Today'.
          it_table[ 2 ]-ztoday = 'Result today'.
          it_table[ 3 ]-ztoday = 'Result Today'.

        WHEN 3.
          it_table[ 1 ]-ztommorrow = 'Result Tomorrow'.
          it_table[ 2 ]-ztommorrow = 'Result Tomorrow'.
          it_table[ 3 ]-ztommorrow = 'Result Tomorrow'.
      ENDCASE.

      lv_count = lv_count + 1.

    ENDDO.

我的想法类似于下面的伪代码。如果it_table的字段达到100(字段),我不想多次执行CASE特殊实例。

   DO 3 TIMES.
      ASSIGN 'ZTODAY' TO <dynamic_fieldname>.
      it_table[ 1 ]-<dynamic_fieldname> = <dynamic_result>.
      it_table[ 2 ]-<dynamic_fieldname> = <dynamic_result>.
      it_table[ 3 ]-<dynamic_fieldname> = <dynamic_result>.
    ENDDO.

请帮助或照亮我。

2 个答案:

答案 0 :(得分:1)

你可以尝试以下方法。我认为它会对你有所帮助。您应该仔细查看命令ASSIGN COMPONENT OF STRUCTURE

TYPES: BEGIN OF lty_dates,
         yesterday TYPE string,
         today     TYPE string,
         tomorrow  TYPE string,
       END OF lty_dates.

TYPES: BEGIN OF lty_result ,
         fieldname TYPE fieldname,
         result    TYPE string,
       END OF lty_result .

FIELD-SYMBOLS <fs_data> TYPE any .

DATA: lt_table  TYPE TABLE OF lty_dates,
      lt_result TYPE TABLE OF lty_result.

lt_result = VALUE #( ( fieldname         = 'yesterday'
                      result = 'result_yesterdaty' )
                      ( fieldname = 'today'
                        result = 'result_today' )
                      ( fieldname = 'tomorrow'
                         result = 'result_tomorrow'
                      ) ).

DO 3 TIMES .
  APPEND INITIAL LINE TO lt_table .
ENDDO .

LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<fs_table>) .
  LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<fs_result>) .
    ASSIGN COMPONENT <fs_result>-fieldname OF STRUCTURE <fs_table> TO <fs_data> .
    MOVE <fs_result>-result TO <fs_data> .
  ENDLOOP .
ENDLOOP .

答案 1 :(得分:1)

实际上,您的代码会创建此结果表:

YESTERDAY           TODAY           TOMORROW
Result Yesterday    Result Today    Result Tomorrow
Result Yesterday    Result today    Result Tomorrow
Result Yesterday    Result Today    Result Tomorrow

为什么不使用宏呢?宏可以完美地满足您的需求:

FIELD-SYMBOLS: <fvalue> TYPE ANY.
DEFINE put_values.
  ASSIGN COMPONENT &1 OF STRUCTURE &2 TO <fvalue>.
  <fvalue> = &3.
END-OF-DEFINITION.

it_table = VALUE #( (  ) (  ) (  ) ).

LOOP AT it_table ASSIGNING FIELD-SYMBOL(<fs_tab>).
  put_values 'yesterday' <fs_tab> 'Result Yesterday'.
  put_values 'today' <fs_tab> 'Result Today'.
  put_values 'tomorrow' <fs_tab> 'Result Tomorrow'.
ENDLOOP.

如果循环迭代的数字等于itab的行数(如代码中所示),这将有效。