11-09-2018 3:08 PM
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,
11-09-2018 3:11 PM
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?
11-09-2018 3:45 PM
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.
11-09-2018 3:26 PM
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
11-09-2018 3:54 PM
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>).
11-09-2018 5:13 PM
maheshkumar.palavalli , I will try this method. thanks for your suggestion.
11-09-2018 3:48 PM
Another option would be to declare a table, add 60 lines using a loop, and then access each line using an index.
11-09-2018 4:01 PM
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.
11-09-2018 4:43 PM
bzakaria I haven't done it myself yet, but found some websites that might help:
https://sapcodes.com/2015/11/19/passing-internal-table-records-from-driver-program-to-script-text/
https://wiki.scn.sap.com/wiki/display/Snippets/SAP+SCRIPT+Passing+an+internal+table
11-09-2018 5:15 PM
11-09-2018 10:32 PM
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?
11-10-2018 8:28 AM
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...
11-12-2018 7:59 AM
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.
11-12-2018 2:24 PM
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.
11-12-2018 5:01 PM
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