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: 

Table Comprehensions with Corresponding and Modifying

MichiFr
Participant
0 Kudos

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

1 ACCEPTED SOLUTION

ChristianGünter
Contributor

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

2 REPLIES 2

ChristianGünter
Contributor

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

0 Kudos

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