‎2012 Jul 19 4:31 AM
Hi all,
I want to declare variables dynamically in the sense variable names should be dynamic. But I am not sure how to do it.
as an example,
data gv_index type char3.
do 10 times.
gv_index = sy-index.
concatenate 'var' gv_index into dtype.
data : (dtype) type char10.
enddo.
by the above code I intend to declare variables of type char10 whose names would be var1,var2,var3......var10.
but the system is not allowing me to do like this. can anyone of you please help me out.
Thanks & Regards,
Krishna.
‎2012 Jul 19 5:30 AM
‎2012 Jul 19 6:12 AM
Hello Krishna,
You can define an internal table having a data-reference component to achieve this. And since you want your variables to of static type CHAR10 you need not use the class CL_ABAP_ELEMDESCR as suggested by . Else you need to use the technique mentioned by him.
TYPES:
BEGIN OF ty_dyn_var,
name TYPE string,
data TYPE REF TO data,
END OF ty_dyn_var.
DATA: gv_indx TYPE n LENGTH 2,
gt_dyn_var TYPE SORTED TABLE OF ty_dyn_var WITH UNIQUE KEY name,
gwa_dyn_var TYPE ty_dyn_var.
* Create the data reference for the dyn. variables
CREATE DATA gwa_dyn_var-data TYPE c LENGTH 10.
* Fill the table with the dynamically created variables
DO 10 TIMES.
gv_indx = sy-index.
CONCATENATE 'VAR' gv_indx INTO gwa_dyn_var-name.
INSERT gwa_dyn_var INTO TABLE gt_dyn_var.
ENDDO.
BR,
Suhas
‎2012 Jul 19 6:56 AM
Hello,
If the intention is just to declare the variables dynamically, then your code works with little modification as below:
REPORT ztest_chmuk.
DATA gv_index TYPE CHAR3.
DATA : dtype TYPE char10.
DO 10 TIMES.
gv_index = sy-index.
CONDENSE GV_INDEX.
CONCATENATE 'var' gv_index INTO dtype .
ENDDO.
Not sure why were you giving (dtype) and not dtype? However, I think the code shown in the previous reply too should suffice your question!
Regards,
Kumud
‎2012 Jul 19 9:41 AM
In your case you've a static variable dtype will be assigned different values - var1, var2, ..., var10. How are 10 different variables created at runtime as required by the OP?
‎2012 Jul 19 9:55 AM
Ok, so the question is not to declare 10 variables rather he is talking of defining something similar to an array! Yep, in that case what have I suggested is incorrect . Then internal table with name-value pair field would be required.
Thanks.
Kumud
‎2012 Jul 19 7:11 AM
you can also do it like below, but i do not like it this way
define test.
data:&1(&2) type c.
end-of-definition.
test var 10.
var = '1234567890'.
write var.
‎2012 Jul 19 9:28 AM