09-28-2013 10:07 PM
Hello expert,
I have one strcture defined which has fields = field01, field02 , field03, field04, field05, ..... field100
Data: ls_field type <structure type>
lt_field type table of ls_field.
data: lv_fieldName type string. // hold the value of field name .. field01 or field02 or field03 or ..... field100
I am passing 01, 02, 03 .... as input
so my lv_fieldname will be = field01 or field02 or field03.
for exaple, lv_fieldName = 'field02'.
Now, I am looping table lt_field. which is populated.
Loop lt_field into ls_field.
write ls_field-lv_fieldName .
// program is giving error at this point... my question is , i want to get the value dynamically based on string ( lv_fieldName) but lv_fieldName is not part of the structure. so its giving me error.....
Correct way is write ls_field-field01. // can we reference the field name dynamically to get th values.
endLoop.
09-29-2013 4:03 PM
Assign Component of Structure statement can be used.
Look at this snippet.
TYPES:
BEGIN OF ty_test,
field01 TYPE char10,
field02 TYPE char10,
END OF ty_test.
DATA: ls_test TYPE ty_test,
lv_varname TYPE string.
FIELD-SYMBOLS: <fs>.
ls_test-field01 = 1.
ls_test-field02 = 2.
lv_varname = 'FIELD02'.
ASSIGN COMPONENT lv_varname OF STRUCTURE ls_test TO <fs>.
IF sy-subrc EQ 0.
WRITE:/ <fs>.
ENDIF.
09-29-2013 4:03 PM
Assign Component of Structure statement can be used.
Look at this snippet.
TYPES:
BEGIN OF ty_test,
field01 TYPE char10,
field02 TYPE char10,
END OF ty_test.
DATA: ls_test TYPE ty_test,
lv_varname TYPE string.
FIELD-SYMBOLS: <fs>.
ls_test-field01 = 1.
ls_test-field02 = 2.
lv_varname = 'FIELD02'.
ASSIGN COMPONENT lv_varname OF STRUCTURE ls_test TO <fs>.
IF sy-subrc EQ 0.
WRITE:/ <fs>.
ENDIF.