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 run the content of a string variable

former_member254358
Participant
0 Kudos

Hi,

I have a table in which there are eight field with this name:

COLOR1, COLOR2,.....COLOR8

In order to improve the code I would like to do a loop which will execute 8 times and will get the value of the eight fields.

I think the code would be somethig like this.

DATA : MYSUM TYPE I.

DATA : MYSTR(30) TYPE C.

CLEAR MYSUM.

DO 8 TIMES.

     CLEAR MYSTR.

     CONCATENATE "MYTABLE-COLOR"  SY-INDEX INTO MYSTR.

     MYSUM = MYSUM + MYSTR.         

ENDDO.

And my question is whether it is possible to get the value of MYSTR in each loop (MYTABLE-COLOR1, MYTABLE-COLOR2....)?

Thanks a lot.

1 ACCEPTED SOLUTION

rajkumarnarasimman
Active Contributor
0 Kudos

Hi David,


David Roig wrote:

And my question is whether it is possible to get the value of MYSTR in each loop (MYTABLE-COLOR1, MYTABLE-COLOR2....)?

"Add MYSTR in the beginning as shown below.

CONCATENATE     MYSTR

                                   "MYTABLE-COLOR" 

                                    SY-INDEX

                          INTO MYSTR.

Regards

Rajkumar Narasimman

7 REPLIES 7

rajkumarnarasimman
Active Contributor
0 Kudos

Hi David,


David Roig wrote:

And my question is whether it is possible to get the value of MYSTR in each loop (MYTABLE-COLOR1, MYTABLE-COLOR2....)?

"Add MYSTR in the beginning as shown below.

CONCATENATE     MYSTR

                                   "MYTABLE-COLOR" 

                                    SY-INDEX

                          INTO MYSTR.

Regards

Rajkumar Narasimman

0 Kudos

Hi,

I'm sorry because I haven't explained clearly.

For example, MYTABLE has the following record:

COLOR1  COLOR2  COLOR3    ............. COLOR8

======================================

BROWN   BLACK     YELLOW                WHITE

It is like two loops nested. The first one will returns the records of MYTABLE and

the second one will execute 8 times, one for each COLOR.

In the second loop will mount this string "MYTABLE-COLOR" + SY-INDEX and with this string,

for example MYTABLE-COLOR1, I should be able to return the value BROWN and the same for the rest of field.

I don't know whether it is possible.

Thanks a lot.

0 Kudos

Check below.....


0 Kudos

Hi David,

To fetch the color brown from MYTABLE, you can retrieve like this.

Method1 - using index value



FIELD-SYMBOLS: <FS_VAL> TYPE ANY.

LOOP AT MYTABLE INTO WA_TABLE.

DO 8 TIMES.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE WA_TABLE TO <fs_val>.


ENDDO.


ENDLOOP.


Method2 - using Field name value


FIELD-SYMBOLS: <FS_VAL> TYPE ANY.

LOOP AT MYTABLE INTO WA_TABLE.

DO 8 TIMES.


CONCATENATE    "MYTABLE-COLOR"

                                    SY-INDEX

                          INTO MYSTR.

ASSIGN COMPONENT (MYSTR) OF STRUCTURE WA_TABLE TO <fs_val>.

ENDDO.

ENDLOOP.

Regards

Rajkumar Narasimman

Former Member
0 Kudos

Are you referring to

Do 8 times varying syntax?

0 Kudos

This would be equivalent:

.....................

write : MYTABLE-COLOR1.

write : MYTABLE-COLOR2.

write : MYTABLE-COLOR3.

write : MYTABLE-COLOR4.

write : MYTABLE-COLOR5.

write : MYTABLE-COLOR6.

write : MYTABLE-COLOR7.

write : MYTABLE-COLOR8.


to this:


DO 8 TIMES.

     CONCATENATE "MYTABLE-COLOR" SY-INDEX INTO MYSTR.


*This should return the content of the eight field

     WRITE : MYSTR.

ENDDO.


Thanks.

0 Kudos

Hi, in this case I don't think that only a "do while" would solve the problem. If this table contains only the 8 fields with colors. you should do as bellow:

data: lv_field type string.

field-symbols: <lfs_structure> type any,

                      <lfs_field_value> type any.

loop at l_table assigning <lfs_structure>.

    

do 8 times.

     lv_field = 'COLOR'  && sy-tabix.

    

     assign componet lv_field of structure <lfs_structure> to <lfs_field_value>.

     if sy-subrc = 0.

     " here you may write/modify the value of the field symbol <lfs_field_value>.

     " you must know that every "do 8 times" will only show a line of the table

     endif.

enddo.

endloop.

Best regards.