将值分配给动态结构



在下面的代码上需要想法,如何简化。下面的代码效果很好,但是有什么方法可以增强或缩短代码,使其动态?

    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(字段),我不想多次执行案例。

   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.

请帮助或对此点燃我。

您可以使用命令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 .

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

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(如您的代码中),则该数字将有效。

相关内容

  • 没有找到相关文章

最新更新