Jen Mer
Jen Mer

Reputation: 101

Use dynamic structure field in ABAP

I have a variable which should determine which field of a structure I use in further compilations. So assume my variable is named currency and can have the values 'K', 'T' and 'H'. After checking which value it is, I want to use corresponding structure fields, e.g. mystructure-fieldk, mystructure-fieldt, mystructure-fieldi.

As for now, I just use IF..THEN to check the value of my variable currency and then just use the corresponding field of the structure, but therefore I have to repeat the coding of my loop 3 times which is just ugly.

    LOOP AT itab ASSIGNING <fs>.

      mystructure-fieldk = mystructure-fieldk + <fs>-otherfieldk.

    ENDLOOP.

Is there a way that i can use the loop with a dynamic field? As in: IF currency = 'K'. DATA(mydynamicfield) = fieldk. ENDIF. and then use mydynamicfield in the loop:

    LOOP AT itab ASSIGNING <fs>.

      mystructure-mydynamicfield = mystructure-mydynamicfield + <fs>-otherfieldk.

    ENDLOOP.

Or anything else? Thanks a lot!

Upvotes: 0

Views: 21089

Answers (4)

Bartosz Brajewski
Bartosz Brajewski

Reputation: 1

You can try with below code:

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.

Upvotes: 0

Umar Abdullah
Umar Abdullah

Reputation: 1290

I created working example with data. I believe, this will definitely help you. dynamic field values are calculated and result is displayed at the end.

  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 ).

Upvotes: 1

Ravi Andela
Ravi Andela

Reputation: 71

Use the below piece of code

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.

Hope this clarifies.

Let's say you want to compute based on other field in the same structure

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.

Note: the code is written to give an idea on how to proceed further in this issue. As Sandra pointed out this may result in compile issues please correct as necessary. I tried answering the question issuing a mobile device only. I don't have access to a compiler/system currently.

Upvotes: 4

Sandra Rossi
Sandra Rossi

Reputation: 13628

There's a better performance with a static reference to the components:

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.

If you want to simplify the code, you may use a macro (inline code):

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.

Upvotes: 1

Related Questions