在 ABAP 中使用动态结构字段



我有一个变量,它应该确定我在进一步编译中使用结构的哪个字段。因此,假设我的变量被命名为货币,并且可以具有值"K","T"和"H"。在检查了它的值之后,我想使用相应的结构字段,例如mystructure-fieldk,mystructure-fieldt,mystructure-fieldi。

至于现在,我只使用IF.。然后检查我的可变货币的值,然后只使用结构的相应字段,但因此我必须重复我的循环编码 3 次,这太丑陋了。

    LOOP AT itab ASSIGNING <fs>.
      mystructure-fieldk = mystructure-fieldk + <fs>-otherfieldk.
    ENDLOOP.

有没有办法将循环与动态字段一起使用?如: IF 货币 = 'K'。 DATA(mydynamicfield) = fieldk. 恩迪夫。然后在循环中使用MyDynamicfield:

    LOOP AT itab ASSIGNING <fs>.
      mystructure-mydynamicfield = mystructure-mydynamicfield + <fs>-otherfieldk.
    ENDLOOP.

还是别的什么?多谢!

使用以下一段代码

LOOP AT ITAB ASSIGNING <fs-structure>.
*-- determination of field name logic goes here let's say you have 
*-- field name is in variable lv_field
    ASSIGN COMPONENT (lv_field) of STRUCTURE <fs-structure> to <fs-field>.
    IF <fs-field> IS ASSIGNED.
      <fs-field> = 'the value you want to assign'.
    ENDIF.
ENDLOOP.

希望这能澄清。

假设您要基于相同结构中的其他字段进行计算

LOOP AT ITAB ASSIGNING <fs-structure>.
*-- determination of field name logic goes here let's say you have 
*-- field name is in variable lv_field1 and other is lv_field2
    ASSIGN COMPONENT (lv_field1) of STRUCTURE <fs-structure> to <fs-field1>.
    ASSIGN COMPONENT (lv_field2) of STRUCTURE <fs-structure> to <fs-field2>.
    IF <fs-field1> IS ASSIGNED ANDV<fs-field2> IS ASSIGNED .
      <fs-field1> = <fs-field1> + <fs-field1>.
    ENDIF.
ENDLOOP.

注意:编写代码是为了给出如何进一步处理此问题的想法。正如Sandra指出的那样,这可能会导致编译问题,请根据需要进行更正。我尝试仅回答发行移动设备的问题。我目前无法访问编译器/系统。

组件的静态引用性能更好:

TYPES: BEGIN OF ty_structure,
         fieldk TYPE i,
         fieldt TYPE i,
         fieldh TYPE i,
       END OF ty_structure.
DATA: itab        TYPE TABLE OF ty_structure,
      mystructure TYPE ty_structure,
      currency    TYPE c LENGTH 1.
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  CASE currency.
    WHEN 'K'. ADD <fs>-fieldk TO mystructure-fieldk.
    WHEN 'T'. ADD <fs>-fieldt TO mystructure-fieldt.
    WHEN 'H'. ADD <fs>-fieldh TO mystructure-fieldh.
  ENDCASE.
ENDLOOP.

如果要简化代码,可以使用宏(内联代码):

LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  DEFINE m_add.
    IF currency = 'K'.
      ADD <fs>-field&1 TO mystructure-field&1.
    ENDIF.
  END-OF-DEFINITION.
  m_add : K, T, H.
ENDLOOP.

我用数据创建了工作示例。我相信,这肯定会对你有所帮助。计算动态字段值,并在末尾显示结果。

  TYPES: BEGIN OF ty_itab,
         currency TYPE c,
         fieldk TYPE i,
         fieldt TYPE i,
         fieldh TYPE i,
       END OF ty_itab.
  TYPES: BEGIN OF ty_structure,
         fieldk TYPE i,
         fieldt TYPE i,
         fieldh TYPE i,
       END OF ty_structure.
DATA: itab   TYPE TABLE OF ty_itab,
      ls_structure TYPE ty_structure.
itab = VALUE #( ( currency = 'H' fieldk = 10 fieldt = 20 fieldh = 30 )
                ( currency = 'H' fieldk = 50 fieldt = 60 fieldh = 70 )
                ( currency = 'T' fieldk = 100 fieldt = 120 fieldh = 150 )
                ( currency = 'K' fieldk = 300 fieldt = 400 fieldh = 500 )  ).
LOOP AT itab ASSIGNING FIELD-SYMBOL(<fs>).
  CASE <fs>-currency.
    WHEN 'K'. ADD <fs>-fieldk TO ls_structure-fieldk.
    WHEN 'T'. ADD <fs>-fieldt TO ls_structure-fieldt.
    WHEN 'H'. ADD <fs>-fieldh TO ls_structure-fieldh.
  ENDCASE.
ENDLOOP.
cl_demo_output=>display( ls_structure ).

您可以尝试使用以下代码:

TYPES: BEGIN OF ts_basic_line,
     matnr TYPE mara-matnr,
     lifnr TYPE lfa1-lifnr,
   END OF ts_basic_line,
   ty_basic_line TYPE TABLE OF ts_basic_line.
DATA: lt_basic_data TYPE ty_basic_line,
      lo_structure  TYPE REF TO cl_abap_structdescr,
      lt_components TYPE abap_component_tab.
lt_basic_data = VALUE #( ( matnr = '111' lifnr = '333' ) ). " data for test
READ TABLE lt_basic_data ASSIGNING FIELD-SYMBOL(<fs_line>) INDEX 1.
IF sy-subrc EQ 0.
  lo_structure ?= cl_abap_typedescr=>describe_by_data( <fs_line> ).
  lt_components = lo_structure->get_components( ).
  LOOP AT lt_components ASSIGNING FIELD-SYMBOL(<fs_strucutre_fields>).
    LOOP AT lt_basic_data ASSIGNING FIELD-SYMBOL(<fs_data>).
      ASSIGN COMPONENT <fs_strucutre_fields>-name OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_value>).
      IF sy-subrc EQ 0.
        " here your code, e.x. data conversion
        <fs_value> = |{ <fs_value> ALPHA = IN }|.
      ENDIF.
    ENDLOOP.
  ENDLOOP.
ENDIF.

相关内容

  • 没有找到相关文章

最新更新