09-10-2015 2:47 PM
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
09-12-2015 11:49 AM
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
09-12-2015 11:49 AM
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
09-14-2015 12:45 PM
Christian,
that's it! Perfect solution!
The missing part was the LET statement that I've already tried to use, however, I obviously missed somehow that I had to create a base structure and fill the member with the correct value instead of using just a single field and use this one in the CORRESPONDING statement.
Your example saved me from writing some more lines of coding and looping through the data a second time.
Thanks again,
Michael