Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map key-value to structure

Tags:

abap

I have a key-value internal table and want to fill an existing structure with it.

Example: 1st I get a key-value table that I create by reading data from two existing tables.

SELECT vals~attr_value, names~attr_name
  FROM atst_attr AS vals
    INNER JOIN tc_attr AS names
      ON vals~tc_attr_id = names~tc_attr_id
  WHERE vals~atst_id = @lv_atst_id
  INTO TABLE @DATA(itab)
  .

Now my itab looks like this:

itab: 
      name      value  
 1. "field_a" "value_a"  
 2. "field_c" "value_c"

And my local structure (or workingarea as it is often called) is empty:

l_struc: 
   field_a: ""
   field_b: ""
   field_c: ""

Now I want to fill the structure, which is where I need help - I want the result to be:

l_struc: field_a: "value_a" field_b: "" field_c: "value_c"

How can I automatically make the mapping from the name property to the name of the structure component happen and setting its value?

like image 282
Cold_Class Avatar asked Jan 20 '26 13:01

Cold_Class


1 Answers

something like:

FIELD-SYMBOLS lv_field TYPE ANY.

LOOP AT itab
     ASSIGNING FIELD-SYMBOL(<ls_itab>).
  ASSIGN COMPONENT <ls_itab>-name
         OF STRUCTURE l_struc
         TO <lv_field>.
  IF sy-subrc EQ 0.
    <lv_field> = <ls_itab>-value.
  ENDIF.
ENDLOOP.
like image 90
József Szikszai Avatar answered Jan 23 '26 15:01

József Szikszai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!