Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to access a structure field dynamically

Former Member
0 Likes
5,737

hi,

I have a structure say lt_struc which has some 10 fields.

i have a variable lv_field which holds the field name.

how do i fetch the field value for the field name stored in the variable lv_field from the structure lt_struc.

with regards

kiran

1 ACCEPTED SOLUTION
Read only

Former Member
2,797

Use the Field Symbols.

Field-symbols: <f_fs> type any,
                        <f_f1> type any,
                        <f_f2> type any,
                         <f_f3> tye any.

Loop at lt_struc assigning <f_fs>.
ASSIGN COMPONENT 1 OF STRUCTURE <f_fs> TO <f_f1>.
ASSIGN lv_field TO <f_fs2>.
<f_fs1> = <f_fs2>.
endloop.

Regards

Kannaiah

7 REPLIES 7
Read only

Former Member
0 Likes
2,797

I didnt understand what exactly you want for dynamically fetching values you can use FIELD-SYMBOLS for fetching the values

Read only

Former Member
2,798

Use the Field Symbols.

Field-symbols: <f_fs> type any,
                        <f_f1> type any,
                        <f_f2> type any,
                         <f_f3> tye any.

Loop at lt_struc assigning <f_fs>.
ASSIGN COMPONENT 1 OF STRUCTURE <f_fs> TO <f_f1>.
ASSIGN lv_field TO <f_fs2>.
<f_fs1> = <f_fs2>.
endloop.

Regards

Kannaiah

Read only

0 Likes
2,797

Actually i will not be knowing which field to access before hand from the structure. only at run time i will get to know which field to access.

So it is something like i have the field name stored in a variable.

Request to provide code in detail.

thanks

kiran

Read only

0 Likes
2,797

Hi,


ASSIGN COMPONENT comp OF STRUCTURE struc TO <comp>. 

[Assigning Components of Structures to a Field Symbol|http://help.sap.com/saphelp_nw70/helpdata/EN/fc/eb3923358411d1829f0000e829fbfe/frameset.htm]


DATA gs_mara TYPE mara.
DATA gv_fieldname TYPE fieldname VALUE 'MATNR'.
FIELD-SYMBOLS <gv_fieldvalue> TYPE ANY.

ASSIGN COMPONENT gv_fieldname OF STRUCTURE gs_mara TO <gv_fieldvalue>.

Regards Rudi

Read only

0 Likes
2,797

Hi,

Let me be simple in explaining the what I want:

Data: itab type (DDIC structure Name).

Now this DDIC structure Name has 5 fields: f1,f2,f3,f4&f5.

Now I want:

Data: lv_temp type string.

Field-symbols: <lv_dynamic_field> type ANY.

lv_temp = itab-<lv_dynamic_field>. - is this the correct one ?

here lv_dynamic_field can have any field name like f1 or f2 or f3......

Hope you got the issue now ?

Thanks & Regards,

Kiran

Read only

0 Likes
2,797

data: l_fldname type string.

field-symbols: <fs1> type any.

l_fldname = assign the fieldname of the internal table to this string.

assign (l_fldname) to <fs1>.

now you can use this field symbol <fs1> for your further processing.

Hope this would help you.

Read only

0 Likes
2,797

Thanks Rudi, I followed your solution and it worked.

Thank you all for your help.

With regards,

kiran