0

需要对以下代码的想法,如何简化。下面的代码效果很好,但有没有办法可以增强或缩短代码使其动态化?

    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.

请帮助或照亮我。

4

2 回答 2

2

你可以使用命令ASSIGN COMPONENT compname OF STRUCTURE structure TO <field_symbol>

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 <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 Yesterday' )
                     ( fieldname = 'today'
                       result    = 'Result Today' )
                     ( fieldname = 'tomorrow'
                       result    = 'Result Tomorrow' ) ).

lt_table = VALUE #( (  ) (  ) (  ) ). " 3 empty lines

LOOP AT lt_table ASSIGNING FIELD-SYMBOL(<table>) .
  LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<result>) .
    ASSIGN COMPONENT <result>-fieldname OF STRUCTURE <table> TO <data> .
    <data> = <result>-result.
  ENDLOOP .
ENDLOOP .
于 2017-12-14T10:22:57.147 回答
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 的行数(如您的代码中所示),这将起作用。

于 2017-12-15T17:22:58.747 回答