Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Filling table with random numbers.

Hi.

I have a problem with filling table like i said in topic. I cant filling in this case <gt_dyn> with random numbers, and i don't know why. Could someone help me. In below I paste code.

DATA: go_struct  TYPE REF TO cl_abap_structdescr,

       go_table   TYPE REF TO cl_abap_tabledescr,

       gd_dyn     TYPE REF TO data,

       gt_comp    TYPE cl_abap_structdescr=>component_table,

       gs_comp    TYPE cl_abap_structdescr=>component,

       gv_pole    TYPE string,

       gv_counter TYPE string,

       gt_record  TYPE TABLE OF zwcw_task23.

FIELD-SYMBOLS: <gs_dyn>    TYPE ANY,

                <gt_dyn>    TYPE STANDARD TABLE,

                <gs_dyn1>   TYPE ANY,

                <gt_dyn1>   TYPE STANDARD TABLE,

                <gs_dyn2>   TYPE ANY,

                <gt_dyn2>   TYPE STANDARD TABLE,

                <fs_value>  TYPE any.

DATA: int TYPE i,

       x TYPE i,

       p_size TYPE i VALUE 2.

FIELD-SYMBOLS: <fs> TYPE ANY.

DO p_size TIMES.

   ADD 1 TO gv_counter.

   CONCATENATE 'F' gv_counter INTO gv_pole.

   gs_comp-name = gv_pole(2).

   gs_comp-type = cl_abap_elemdescr=>get_i( ).

   APPEND gs_comp TO gt_comp. CLEAR gs_comp.

ENDDO.

TRY.

*   Create the dynamic structure

     go_struct = cl_abap_structdescr=>create( gt_comp ).

     CREATE DATA gd_dyn TYPE HANDLE go_struct. "Create dyn. data ref.

     ASSIGN gd_dyn->* TO <gs_dyn>. "Dereference the data ref.

     CLEAR gd_dyn.

*   Create the dyn. table

     go_table = cl_abap_tabledescr=>create( go_struct ).

     CREATE DATA gd_dyn TYPE HANDLE go_table. "Create dyn. data ref.

     ASSIGN gd_dyn->* TO <gt_dyn>. "Dereference the data ref.

   CATCH cx_sy_struct_creation.

   CATCH cx_sy_table_creation.

ENDTRY.

DO p_size TIMES.

   CALL FUNCTION 'QF05_RANDOM_INTEGER'

     EXPORTING

       ran_int_max   = 100

       ran_int_min   = 1

     IMPORTING

       ran_int       = int

     EXCEPTIONS

       invalid_input = 1

       OTHERS        = 2.

   ASSIGN int TO <fs_value>.

   ASSIGN COMPONENT sy-index OF STRUCTURE <fs_value> TO <gs_dyn>.

   INSERT <gs_dyn> INTO TABLE <gt_dyn>.

ENDDO.

Former Member
replied

Strange, you did the hardest part

  1 ASSIGN int TO <fs_value>.

  2 ASSIGN COMPONENT sy-index OF STRUCTURE <fs_value> TO <gs_dyn>.

  3 INSERT <gs_dyn> INTO TABLE <gt_dyn>.

1 now <fs_value> reference an integer

2 as <fs_value> is not a structure,  guess the sy-subrc (answer 4)

3 don't expect much here

You could try a

INSERT INITIAL LINE INTO TABLE <gt_dyn> ASSIGNING <gs_dyn>.

ASSIGN COMPONENT sy-index OF STRUCTURE  <gs_dyn> TO <fs_value>.

<fs_value> = int.

Adapt to your requirement, as this code will fill one subfield at a time, so move the DO.

Hint: Also, please, add some check on sy-subrc or <fs> IS ASSIGNED in your code...

Regards,

Raymond

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question