我有一个变量,该变量应确定我在进一步的编译中使用的结构的哪个字段。 因此,假设我的变量名为货币,并且可以具有值“ K”,“ T”和“ H”。检查它是哪个值后,我想使用相应的结构字段,例如mystructure-fieldk,mystructure-fieldt,mystructure-fieldi。
就目前而言,我只使用IF..THEN检查我的可变货币的值,然后只使用结构的相应字段,但是因此我必须重复3次循环编码,这很丑陋
LOOP AT itab ASSIGNING <fs>.
mystructure-fieldk = mystructure-fieldk + <fs>-otherfieldk.
ENDLOOP.
有没有一种方法可以将循环与动态字段一起使用?如: 如果货币='K'。 DATA(mydynamicfield)= fieldk。 万一。 然后在循环中使用mydynamicfield:
LOOP AT itab ASSIGNING <fs>.
mystructure-mydynamicfield = mystructure-mydynamicfield + <fs>-otherfieldk.
ENDLOOP.
还是其他?非常感谢!
答案 0 :(得分:3)
使用下面的代码
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指出的那样,这可能会导致编译问题,请根据需要进行更正。我尝试回答仅发行移动设备的问题。我目前无法访问编译器/系统。
答案 1 :(得分:1)
通过静态引用这些组件可以获得更好的性能:
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.
答案 2 :(得分:1)
我用数据创建了工作示例。我相信,这一定会对您有所帮助。 计算动态字段值,并在最后显示结果。
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 ).
答案 3 :(得分:0)
您可以尝试使用以下代码:
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.