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

help - assign component

Former Member
0 Likes
458

Hi,

i need your ideas for an assign component issue.

data definition:

*table

it1_tab: (all fields are char1: field1, field2, ...field99)

*local fields

lv_fieldx1 type string.

lv_fieldx2 type string.

...

lv_fieldx99 type string.

Now i need to check "field1 of table with localfield1" and "field2 of table with localfield2" ...until field99).

Example with IF step.

if it_tab-field1 = 'X'.

lv_fieldx1 = '1'.

else.

lv_fieldx1 = '0'.

endif.

if it_tab-field2 = 'X'.

lv_fieldx2 = '1'.

else.

lv_fieldx2 = '0'.

endif.

...

if it_tab-field99 = 'X'.

lv_fieldx99 = '1'.

else.

lv_fieldx99 = '0'.

endif.

I think there is a better way by using assign component in a Do step ?

any ideas ?

Regards,

Gordon

1 ACCEPTED SOLUTION
Read only

JozsefSzikszai
Active Contributor
0 Likes
424

something like (may contain errors):

DATA : lv_index(2) TYPE n.
DATA : lv_fieldname1 TYPE fieldname.
DATA : lv_fieldname2 TYPE fieldname.
FIELD-SYMBOLS : <lv_field1> TYPE ANY.
FIELD-SYMBOLS : <lv_field2> TYPE ANY.

LOOP AT itab.
DO 99 TIMES.
lv_index = sy-index.
CONCATENATE 'FIELD' lv_index INTO lv_fieldname1.
CONCATENATE 'LV_FIELDX' lv_index INTO lv_fieldname2
ASSIGN COMPONENT (lv_fieldname1) OF STRUCTURE itab TO <lv_field>.
ASSIGN (lv_fieldname2) TO <lv_field2>.
IF <lv_field1> IS NOT ASSIGNED OR
<lv_field2> IS NOT ASSIGNED.
CONTINUE.
ENDIF.
IF <lv_field> EQ 'X'.
<lv_field2> = 'X'.
ELSE.
<lv_field2> = ' '.
ENDIF.
ENDDO.
ENDLOOP.

There will be definetly one more problem: The genrated field names will look like this: field01, field02, field03... There are two solutions: 1. You change the declaration accordingly 2. You cut the leading zeros from lv_index

2 REPLIES 2
Read only

JozsefSzikszai
Active Contributor
0 Likes
425

something like (may contain errors):

DATA : lv_index(2) TYPE n.
DATA : lv_fieldname1 TYPE fieldname.
DATA : lv_fieldname2 TYPE fieldname.
FIELD-SYMBOLS : <lv_field1> TYPE ANY.
FIELD-SYMBOLS : <lv_field2> TYPE ANY.

LOOP AT itab.
DO 99 TIMES.
lv_index = sy-index.
CONCATENATE 'FIELD' lv_index INTO lv_fieldname1.
CONCATENATE 'LV_FIELDX' lv_index INTO lv_fieldname2
ASSIGN COMPONENT (lv_fieldname1) OF STRUCTURE itab TO <lv_field>.
ASSIGN (lv_fieldname2) TO <lv_field2>.
IF <lv_field1> IS NOT ASSIGNED OR
<lv_field2> IS NOT ASSIGNED.
CONTINUE.
ENDIF.
IF <lv_field> EQ 'X'.
<lv_field2> = 'X'.
ELSE.
<lv_field2> = ' '.
ENDIF.
ENDDO.
ENDLOOP.

There will be definetly one more problem: The genrated field names will look like this: field01, field02, field03... There are two solutions: 1. You change the declaration accordingly 2. You cut the leading zeros from lv_index

Read only

0 Likes
424

Thx,

it was a good try !

Im using only 9 fields in my live example;)


DATA : lv_index(1) TYPE n.
DATA : lv_fieldname1 TYPE fieldname.
DATA : lv_fieldname2 TYPE fieldname.
FIELD-SYMBOLS : <lv_field1> TYPE ANY.
FIELD-SYMBOLS : <lv_field2> TYPE ANY.
 
LOOP AT itab.
DO 9 TIMES.
lv_index = sy-index.
CONCATENATE 'FIELD' lv_index INTO lv_fieldname1.
CONCATENATE 'LV_FIELDX' lv_index INTO lv_fieldname2.
ASSIGN COMPONENT lv_fieldname1 OF STRUCTURE itab TO <lv_field>.
ASSIGN (lv_fieldname2) TO <lv_field2>.
IF <lv_field1> IS NOT ASSIGNED OR
<lv_field2> IS NOT ASSIGNED.
CONTINUE.
ENDIF.
IF <lv_field> EQ 'X'.
<lv_field2> = 'X'.
ELSE.
<lv_field2> = ' '.
ENDIF.
ENDDO.
ENDLOOP.

Thx,

Gordon