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

Former Member
0 Kudos
12,514

Hi All,

Here is my fishy requirement.

I have tens of variables globally declared as g_line_01,g_line_02 and so on to g_line_99. (I can not change this declaration part as it used by other part of the programs.

Now I need to fillin data into these g_line_xx lines which will then print into a dataset.

Now my requirement is here:

Lines g_line_13 to g_line_17 should contain the customer address information, ie. Name1, Name2, Name3, Name4, Street, City, PO Box and Postal Code, country etc.

Now there exist a logic to group these fields and populate the lines from g_line_13 to g_line_17. The problem comes when one/some of the above said field/s is/are blank. That may result in one of the g_line_xx variable to be blank. I need to avoid this scenario. That means, if I see that g_line_15 is to be blank, then all the below line should come up by one line.

I was thinking can we have this done by dynamic variable names ?? ie. I use some string manupulation to prepare the variable name g_line_xx and can I have this in the assignment statement ?? Or is it impossible?

1 ACCEPTED SOLUTION

Former Member
1,625

Hi Karthik,

If I have understood your requirement correctly, then I think what you want to do is possible. As an example I consider that you have the following data assignments currently -

g_line_13 = VAR1. " 'ABC'
g_line_14 = VAR2. " 'DEF'
g_line_15 = VAR3  " '' (BLANK).
g_line_16 = VAR4  " 'JKL'.
g_line_17 = VAR5  " 'MNO'.

Now the following code will move the data to the appropriate variable (taking into consideration the variable number) only when the source data field has got some content.

DATA:  LV_FIELDNAME(9)   TYPE C,
       LV_PREFIX(7)      TYPE C VALUE 'G_LINE_'
       LV_LINE_NUMBER(2) TYPE N VALUE '12'.

FIELD-SYMBOLS : <FS_GLOBAL_VARIABLE>.



IF VAR1 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR2 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.


FORM MOVE_VARIABLES USING VAR1.
  ADD 1 TO LV_LINE_NUMBER.
  CONCATENATE LV_PREFIX 
              LV_LINE_NUMBER
         INTO LV_FIELDNAME.
  ASSIGN (LV_FIELDNAME) TO <FS_GLOBAL_VARIABLE>.
  <FS_GLOBAL_VARIABLE> = VAR1.
ENDFORM.

Let me know if that helps. Or if you need some other information.

Regards,

Anand Mandalika.

7 REPLIES 7

Former Member
0 Kudos
1,625

Hi Karthick,

I'd suggest a simpler solution.

Instead of declaring them as variables, try to use Internal table with 99 fields. Then it is easy to shfit them up or down accordingly rt?

Cheers,

Sam

0 Kudos
1,625

Samuel,

As I have clearly mentioned, we can not change the declaration part any more..

Anyway thanks for the reply

0 Kudos
1,625

Hi Karthick,

I understand that. But still, you can use an internal table that would actually contain the manipulated (move up/down) values based on blank field values. Later, at the end of the modification process, just move the values from the internal table to the corresponding global variables defined already.

Cheers,

Sam

Former Member
0 Kudos
1,625

Can use coding like this.

DATA: ITAB OCCURS O,

g_line_01(100),

g_line_02(100),

.

.

.

g_line_99(100),

DATA: END OF ITAB.

READ TABLE ITAB...

Cheers,

Sam

Former Member
1,626

Hi Karthik,

If I have understood your requirement correctly, then I think what you want to do is possible. As an example I consider that you have the following data assignments currently -

g_line_13 = VAR1. " 'ABC'
g_line_14 = VAR2. " 'DEF'
g_line_15 = VAR3  " '' (BLANK).
g_line_16 = VAR4  " 'JKL'.
g_line_17 = VAR5  " 'MNO'.

Now the following code will move the data to the appropriate variable (taking into consideration the variable number) only when the source data field has got some content.

DATA:  LV_FIELDNAME(9)   TYPE C,
       LV_PREFIX(7)      TYPE C VALUE 'G_LINE_'
       LV_LINE_NUMBER(2) TYPE N VALUE '12'.

FIELD-SYMBOLS : <FS_GLOBAL_VARIABLE>.



IF VAR1 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR2 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.

IF VAR3 IS NOT INITIAL.
  PERFORM MOVE_VARIABLE USING VAR1.
ENDIF.


FORM MOVE_VARIABLES USING VAR1.
  ADD 1 TO LV_LINE_NUMBER.
  CONCATENATE LV_PREFIX 
              LV_LINE_NUMBER
         INTO LV_FIELDNAME.
  ASSIGN (LV_FIELDNAME) TO <FS_GLOBAL_VARIABLE>.
  <FS_GLOBAL_VARIABLE> = VAR1.
ENDFORM.

Let me know if that helps. Or if you need some other information.

Regards,

Anand Mandalika.

0 Kudos
1,625

Sam,

Great solution.. I had never thought about it.. Thanks a lot.

Anand,

Ultimate answer.. It worked... Thanks a lot. I use this solution...

Former Member
0 Kudos
1,625

could you give me the correct code