Skip to Content

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

Table Comprehensions with Corresponding and Modifying

Hi,

I've written the following simple coding for demo purposes.

The intention is to copy an internal table into a target table and adding/modifying a member (field3) of each table line that gets copied.

TYPES:     

      BEGIN OF t_struct1,       

          field1      TYPE i,        

          field2      TYPE string,      

     END OF t_struct1,      

     BEGIN OF t_struct2,       

          field1      TYPE i,        

          field2      TYPE string,        

          field3      TYPE i,      

     END OF t_struct2.  

DATA:     

     lt_source TYPE STANDARD TABLE OF t_struct1,     

     lt_target TYPE STANDARD TABLE OF t_struct2.   

* Initialize source table with some random values

lt_source = VALUE #(    

      ( field1 = 1 field2 = 'A' )     

     ( field1 = 2 field2 = 'B' ) ).    

* Now copy source lines to target table using corresponding

lt_target = VALUE #( FOR wa IN lt_source ( CORRESPONDING #( wa ) ) ).

The result is that lt_target contains two lines where field1 and field2 is populated. That's perfect.

Now I would like the same construct as in line 19 and have field3 assigned a value. I've tried to add a new statement like field3 = sy-tabix, however this results in a syntax error.

Questions is how can I still use table comprehensions, take advantage about the corresponding constructor and yet modify a target structure member?

Thanks,

Michael

Tags:
replied

Hi Michael,

i think you can solve this with an auxillary let binding and the base addition of corresponding.

Like this

lt_target = VALUE #( FOR wa IN lt_source
                            INDEX INTO index
                            LET base = VALUE t_struct2( field3 = index )
                            IN ( CORRESPONDING #( BASE ( base ) wa ) ) ).

cl_demo_output=>display( lt_target ).

result

Regards Christian

1 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question