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: 

Dynamic Variable names

bzakaria
Participant

Hi,

I have in my program sixty variables which have similar names, except the last character which is incriminated. for example :

var1, var2, var3 ..... var60

I want to declare them using a loop instead of declaring each variable. using an index that increments and concatenates with the unchangeable portion of variables (var...).

Is there a way to do it ? and how?

regards,

14 REPLIES 14

0 Kudos

Do you really want to declare the variables, are you generating a report or class? Or have you already declared them using DATA: ... and want to access each variable at runtime dynamically?

0 Kudos

I wish instead to put:

DATA:

var1 TYPE I,

var2 TYPE I,

var3 TYPE I,

... ...

var60 TYPE I.

to declare them in a loop since they have the same type and same format.

these variables will be declared and filled in a program (in se38) and read in a sapscript.

maheshpalavalli
Active Contributor

Dear Zakaria,

I don't think you can declare variables using a loop and end loop or infact any other loop.

So how to solve your issue, I would recommend you to create a dynamic structure with multiple columns..so instead of multiple variables, you will have one structure with multiple fields, which solves your purpose.. for this you can use RTTS concepts in abap..

https://blogs.sap.com/2014/04/03/create-dynamic-table-using-rtts-and-display-in-alv/

BR,

Mahesh

0 Kudos

You can use the below code:



DATA(lo_var) = CAST cl_abap_datadescr( cl_abap_typedescr=>describe_by_name( 'CHAR10' ) ). " Data element

DATA:

  lo_struct     TYPE REF TO data,

  lt_components TYPE abap_component_tab.



FIELD-SYMBOLS <fs_struct>.



DO 10 TIMES.

  INSERT VALUE #(

      name = |VAR_{ sy-index }|

      type = lo_var )

    INTO TABLE lt_components.

ENDDO.



DATA(lo_structdescr) = cl_abap_structdescr=>get( lt_components ).

CREATE DATA lo_struct TYPE HANDLE lo_structdescr.

ASSIGN lo_struct->* to <fs_struct>.



ASSIGN COMPONENT 'VAR_1' of STRUCTURE <fs_struct> to FIELD-SYMBOL(<fs_var>).


0 Kudos

maheshkumar.palavalli , I will try this method. thanks for your suggestion.

0 Kudos

Another option would be to declare a table, add 60 lines using a loop, and then access each line using an index.

0 Kudos

jascha.kanngiesser , I can pass this table to sapscript? because I did not find how to pass a table of data to a sapscript to display it.

0 Kudos

jascha.kanngiesser , thank you for your answers and suggestions.

matt
Active Contributor

If your program design calls for 60 similarly named variables, your design is wrong. It's broken. In common design terminology - your code "smells" bad.

Probably there is a solution using an internal table, data references and/or RTTS.

Why do you think you need 60 similarly named variables? What are you going to do with them?

0 Kudos

Yeah I also thinking the same while I was giving the RTTS solution. Maybe if zakaria bi shares the reason for that requirement there can be a easier solution instead of creating that many variables...

0 Kudos

matthew.billingham & maheshkumar.palavalli ,

The reason is that I have a table of 10 rows and 6 columns of data. and I have to display it on a table in a sapscript.

The solution I found for displaying a data table in a sapscript is to pass the data via variables. each variable carries a value.

I would like to find another very optimal solution.

Tomas_Buryanek
Active Contributor

bzakaria that is wrong solution. You can print values from internal table. It is usually done with loop on itab to work area and printing each column of that work area (in your case you will print 6 fields in each loop iteration). This way you will need just 1 variable - work area for itab. You can find many examples on the internet.

-- Tomas --

maheshpalavalli
Active Contributor
0 Kudos

You can check the below link as you want to display an internal table in using script.

http://saptechnical.com/Tutorials/SAPScripts/InternalTable/Display.htm