Application Development and Automation 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: 
Read only

"dynamic" variable

Former Member
0 Likes
2,393

Dear experts,

I have an cust. table where once per week from 1 to 3 different values will be written.

Currently I have 3 variables for each table field and I do following:

SELECT *

   FROM zdyu_vstel

      INTO CORRESPONDING FIELDS OF TABLE ta_dyu_vstel.

LOOP AT ta_dyu_vstel

    INTO wa_dyu_vstel.

   IF sy-tabix = 1.

     v_vstel_1 = wa_dyu_vstel-vstel.

   ENDIF.

   IF sy-tabix = 2.

     v_vstel_2 = wa_dyu_vstel-vstel.

   ENDIF.

   IF sy-tabix = 2.

     v_vstel_3 = wa_dyu_vstel-vstel.

   ENDIF.

ENDLOOP.


Could you give me some advise how I can use just one variable in my program which would loop the table and in depends of the ammount of the fields buil itself containing every unique field?


Thank you in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,354

Hi

I don't know if I've understood exaclty what you need, but if your result has to be:


CONCATENATE sv_vstel_1 v_vstel_2 v_vstel_3

   INTO v_string

      SEPARATED BY '-'.

You can use only V_STRING:


DATA: L_OFFSET TYPE I.

L_OFFSET = 0.

LOOP AT ta_dyu_vstel

    INTO wa_dyu_vstel.

  

    IF NOT V_STRING IS INITIAL.

         V_STRING+L_OFFSET = '-'.

         L_OFFSET = L_OFFSET + 1.

    ENDIF.

  

    V_STRING+L_OFFSET(4) = wa_dyu_vstel-vstel.

    L_OFFSET                           = L_OFFSET + 4.

Max

Dear experts,

I have an cust. table where once per week from 1 to 3 different values will be written.

Currently I have 3 variables for each table field and I do following:

SELECT *

   FROM zdyu_vstel

      INTO CORRESPONDING FIELDS OF TABLE ta_dyu_vstel.

LOOP AT ta_dyu_vstel

    INTO wa_dyu_vstel.

   IF sy-tabix = 1.

     v_vstel_1 = wa_dyu_vstel-vstel.

   ENDIF.

   IF sy-tabix = 2.

     v_vstel_2 = wa_dyu_vstel-vstel.

   ENDIF.

   IF sy-tabix = 2.

     v_vstel_3 = wa_dyu_vstel-vstel.

   ENDIF.

ENDLOOP.


Could you give me some advise how I can use just one variable in my program which would loop the table and in depends of the ammount of the fields buil itself containing every unique field?


Thank you in advance.

9 REPLIES 9
Read only

Former Member
0 Likes
2,354

Hi

You should explain us what you need to do with V_VSTLE_x variables else it's not easy to give you a solution

Max

Read only

0 Likes
2,354

Hi Max,

the point is simple. I use theese variables as a part of the file name I sent as a spool.

I have 2 reports: the first report check the information about shipping points and save the results in the table with following structure:

The second report sent the email with some information from the table above and as a name for the file I want use the shipping point, but the problem is that sometimes as a result of the first report i have only 1 shipping point and sometimes 3.

So I have created 3 different variables for each shipping point and do the code above and after I get the variables I do this:

CONCATENATE sv_vstel_1 v_vstel_2 v_vstel_3

   INTO v_string

      SEPARATED BY '-'.

So the question: would it be possible just to have one variable which would contain exactly vstel, which are to find in the result table?

I hope it is understandable.

Read only

0 Likes
2,354

I don't know exactly what you need. But if you really need to use v_vstle_x variables, you can do it:

REPORT zmrl_tst.

DATA: lv_1 TYPE string, "Your variable v_vstel_1

       lv_2 TYPE string, "Your variable v_vstel_2

       lv_3 TYPE string, "Your variable v_vstel_2

       lv_i TYPE i VALUE 1,

       lv_var TYPE string.

FIELD-SYMBOLS: <fs_lv>.

WHILE lv_i LT 4. "loop at your internal table

   lv_var = lv_i. "here you can put lv_tabix = sy-tabix. Declare lv_tabix as text variable

   CONCATENATE 'lv_' lv_var INTO lv_var. "here you put concatenate 'v_vstel_' lv_tabix into lv_var.

   ASSIGN (lv_var) TO <fs_lv>.

   <fs_lv> = lv_i. "here you put <fs_lv> = wa_dyu_vstel-vstel.

   ADD 1 TO lv_i. "you don't need this.

ENDWHILE.

WRITE: lv_1, lv_2, lv_3.

Read only

rosenberg_eitan
Active Contributor
0 Likes
2,354

Hi,

Another attempt...

  • FORM test02 .
  •    DATA: BEGIN OF st_vstel .
  •    DATA: v_vstel_1 TYPE vstel .
  •    DATA: v_vstel_2 TYPE vstel .
  •    DATA: v_vstel_3 TYPE vstel .
  •    DATA: END OF st_vstel .
  •    DATA: it_vstel LIKE TABLE OF st_vstel .
  •    DO 4 TIMES .
  •      APPEND INITIAL LINE TO it_vstel .
  •    ENDDO .
  •    FIELD-SYMBOLS: <st_vstel> LIKE LINE OF it_vstel .
  •    FIELD-SYMBOLS: <vstel> TYPE vstel .
  •    DATA: fieldname TYPE fieldname .
  •    DATA: index TYPE n LENGTH 1 .
  •    LOOP AT it_vstel ASSIGNING <st_vstel>  .
  •      index = sy-tabix  .
  •      CONCATENATE 'V_VSTEL_' index INTO fieldname .
  •      ASSIGN COMPONENT fieldname OF STRUCTURE <st_vstel> TO <vstel> .
  •      CHECK sy-subrc EQ 0 .
  •      <vstel> = 'val' .
  •    ENDLOOP .
  •    BREAK-POINT .
  • ENDFORM .                                                   "test02

Read only

AnoopMayamkote
Participant
0 Likes
2,354

you can use <FS> to acheive this

Read only

Former Member
0 Likes
2,355

Hi

I don't know if I've understood exaclty what you need, but if your result has to be:


CONCATENATE sv_vstel_1 v_vstel_2 v_vstel_3

   INTO v_string

      SEPARATED BY '-'.

You can use only V_STRING:


DATA: L_OFFSET TYPE I.

L_OFFSET = 0.

LOOP AT ta_dyu_vstel

    INTO wa_dyu_vstel.

  

    IF NOT V_STRING IS INITIAL.

         V_STRING+L_OFFSET = '-'.

         L_OFFSET = L_OFFSET + 1.

    ENDIF.

  

    V_STRING+L_OFFSET(4) = wa_dyu_vstel-vstel.

    L_OFFSET                           = L_OFFSET + 4.

Max

Read only

0 Likes
2,354

Hi,

Unfortunately I get the error message:

At the write position, you cannot use offset and lenght specifixcations with fields of type "STRING"

or "XSTRING".

Read only

0 Likes
2,354

Hi

You've defined V_STRING TYPE STRING, that means your variable is a string with indefinite length, so try to define it as string having a certain lenght

DATA: V_STRING(N) TYPE C

N should be the max lenght

Max

Read only

Former Member
0 Likes
2,354

Hi Denis,

Please try the below code.

I am using your code as base with some modifications.

  DATA :  l_v_vstel TYPE vstel.

Select *

  FROM zdyu_vstel

INTO CORRESPONDING FIELDS OF TABLE ta_dyu_vstel.

LOOP AT ta_dyu_vstel

    INTO wa_dyu_vstel.

   IF sy-tabix EQ 1 .

      l_v_vstel = wa_dyu_vstel-vstel.

   ELSEIF l_v_count GT 1 AND LE 3.

     CONCATENATE l_ v_vstel wa_dyu_vstel-vstel INTO l_v_vstel SEPARATED BY '-'.

   ENDIF.

ENDLOOP.

Hope it helps you.