Application Development 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: 

how to assing value to dynamic fields

Former Member
0 Kudos
109

Hi all,

I have two internal tables like:

data : begin of itab1 occurs 0,

counter type i,

date type sy-datum,

value type bseg-dmbtr,

end of itab1.

data : begin of itab2 occurs 0,

date type sy-datum,

value1 type bseg-dmbtr,

value2 type bseg-dmbtr,

value3 type bseg-dmbtr,

value4 type bseg-dmbtr,

value5 type bseg-dmbtr,

end of itab2.

I have data like :

itab1-counter = 1.

itab1-value = '1000.00'.

append itab1.

itab1-counter = 2.

itab1-value = '1100.00'.

append itab1.

itab1-counter = 3.

itab1-value = '1300.00'.

append itab1.

itab1-counter = 4.

itab1-value = '1400.00'.

append itab1.

itab1-counter = 5.

itab1-value = '1500.00'.

append itab1.

Now I have to prepare itab2 based on the condition like:

If itab1-counter = 1, value should be assigned to itab2-value1,

if itab1-counter = 2, value should be assigned to itab2-value2.

Like that the data should be prepared in itab2.

Could anybody help me how it could be done dynamically.

pls don't suggest to use either IF condition or CASE statement as there may be the cases of having more than 20 counters in itab1 and then 20 VALUE fields in ITAB2.

Thanks in advance.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
82

hi...

THIS IS THE WORKING CODE AND TESTED ONE

REPORT yh1306_teast12345.

DATA : BEGIN OF itab1 OCCURS 0,

counter TYPE i,

date TYPE sy-datum,

value TYPE bseg-dmbtr,

END OF itab1.

DATA : BEGIN OF itab2 OCCURS 0,

date TYPE sy-datum,

value1 TYPE bseg-dmbtr,

value2 TYPE bseg-dmbtr,

value3 TYPE bseg-dmbtr,

value4 TYPE bseg-dmbtr,

value5 TYPE bseg-dmbtr,

END OF itab2.

itab1-counter = 1.

itab1-value = '1000.00'.

APPEND itab1.

itab1-counter = 2.

itab1-value = '1100.00'.

APPEND itab1.

itab1-counter = 3.

itab1-value = '1300.00'.

APPEND itab1.

itab1-counter = 4.

itab1-value = '1400.00'.

APPEND itab1.

itab1-counter = 5.

itab1-value = '1500.00'.

APPEND itab1.

DATA: w_char(20) ,

w_char1(5).

FIELD-SYMBOLS : <fs> TYPE ANY.

LOOP AT itab1.

clear itab2.

CLEAR w_char.

w_char = 'itab2-value'.

W_CHAR1 = ITAB1-COUNTER.

CONCATENATE w_char w_char1 INTO w_char .

CONDENSE w_char NO-GAPS.

ASSIGN (w_char) TO <fs>.

<fs> = itab1-value.

APPEND itab2.

UNASSIGN <fs>.

ENDLOOP.

LOOP AT itab2.

WRITE: / itab2-value1,itab2-value2,itab2-value3,itab2-value4,itab2-value5.

ENDLOOP.

6 REPLIES 6

Former Member
0 Kudos
82

Hi,

Try to use LOOP....END LOOP statement on itab1.

and read the itab for counter using read statement.

after the read statement check for the value of counter and accordingly assign values to itab2.

Hope it will help.

Regards

Rajesh Kumar

Former Member
0 Kudos
82

Hi,

try using sy-tabix and compare .

former_member226203
Active Contributor
0 Kudos
82

sort itab1 by counter.

loop at itab1 into wa_itab1.

read itab1 into wa with key counter = wa_itab1-counter.

if sy-subrc = 0.

itab2-<value1> = wa_itab1-value.

" use field symbol so that it takes the value based on the counter ie.,if counter is 1 it will gotoval1, 2 to val2...etc

else.

continue.

endif.

Former Member
0 Kudos
82

Hi Jaya,

Kindly go through these links below:

Hope it helps.

Regrds

Mansi

Former Member
0 Kudos
83

hi...

THIS IS THE WORKING CODE AND TESTED ONE

REPORT yh1306_teast12345.

DATA : BEGIN OF itab1 OCCURS 0,

counter TYPE i,

date TYPE sy-datum,

value TYPE bseg-dmbtr,

END OF itab1.

DATA : BEGIN OF itab2 OCCURS 0,

date TYPE sy-datum,

value1 TYPE bseg-dmbtr,

value2 TYPE bseg-dmbtr,

value3 TYPE bseg-dmbtr,

value4 TYPE bseg-dmbtr,

value5 TYPE bseg-dmbtr,

END OF itab2.

itab1-counter = 1.

itab1-value = '1000.00'.

APPEND itab1.

itab1-counter = 2.

itab1-value = '1100.00'.

APPEND itab1.

itab1-counter = 3.

itab1-value = '1300.00'.

APPEND itab1.

itab1-counter = 4.

itab1-value = '1400.00'.

APPEND itab1.

itab1-counter = 5.

itab1-value = '1500.00'.

APPEND itab1.

DATA: w_char(20) ,

w_char1(5).

FIELD-SYMBOLS : <fs> TYPE ANY.

LOOP AT itab1.

clear itab2.

CLEAR w_char.

w_char = 'itab2-value'.

W_CHAR1 = ITAB1-COUNTER.

CONCATENATE w_char w_char1 INTO w_char .

CONDENSE w_char NO-GAPS.

ASSIGN (w_char) TO <fs>.

<fs> = itab1-value.

APPEND itab2.

UNASSIGN <fs>.

ENDLOOP.

LOOP AT itab2.

WRITE: / itab2-value1,itab2-value2,itab2-value3,itab2-value4,itab2-value5.

ENDLOOP.

former_member222860
Active Contributor
0 Kudos
82

Hi,

Here's the sample code:

data : begin of itab1 occurs 0,
counter type i,
date type sy-datum,
value type bseg-dmbtr,
end of itab1.

data : begin of itab2 occurs 0,
date type sy-datum,
value1 type bseg-dmbtr,
value2 type bseg-dmbtr,
value3 type bseg-dmbtr,
value4 type bseg-dmbtr,
value5 type bseg-dmbtr,
end of itab2.

DATA: field1 type string.
data: c_int(10) type c.

itab1-counter = 1.
itab1-value = '1000'.
append itab1.

itab1-counter = 2.
itab1-value = '1100'.
append itab1.

itab1-counter = 3.
itab1-value = '1300'.
append itab1.

itab1-counter = 4.
itab1-value = '1400'.
append itab1.

itab1-counter = 5.
itab1-value = '1500'.
append itab1.

LOOP AT ITAB1.
READ TABLE ITAB1 WITH KEY COUNTER = SY-TABIX.
c_int = sy-tabix.
CONCATENATE 'ITAB2-VALUE' c_int INTO field1.
CONDENSE field1 no-gaps.
MOVE ITAB1-VALUE TO field1.
APPEND ITAB2.
ENDLOOP.

LOOP AT ITAB2.
WRITE:/ ITAB2-VALUE1, ITAB2-VALUE2, ITAB2-VALUE3.
ENDLOOP.

thanks\

Mahesh