I recently had to rewrite a Report where I saw code that looked similar to this.
TYPES:
BEGIN OF ty_employee,
vorna TYPE vorna,
nachn TYPE nachn,
END OF ty_employee,
tt_employee TYPE TABLE OF ty_employee WITH DEFAULT KEY,
BEGIN OF ty_source,
col1 TYPE c LENGTH 2,
col2 TYPE c LENGTH 2,
col3 TYPE c LENGTH 2,
col4 TYPE c LENGTH 2,
END OF ty_source,
tt_source TYPE TABLE OF ty_source WITH DEFAULT KEY,
BEGIN OF ty_target,
col1 TYPE c LENGTH 2,
col2 TYPE c LENGTH 2,
col3 TYPE c LENGTH 2,
col4_vorna TYPE vorna,
col5_nachn TYPE nachn,
END OF ty_target,
tt_target TYPE TABLE OF ty_target WITH DEFAULT KEY.
* Test employees
DATA(employees) = VALUE tt_employee( ( vorna = 'Max' nachn = 'Mustermann' )
( vorna = 'Erika' nachn = 'Mustermann' ) ).
* In reallity, this table changed per employee
DATA(source_tab) = VALUE tt_source( ( col1 = 'a1' col2 = 'b1' col3 = 'c1' col4 = 'd1' )
( col1 = 'a2' col2 = 'b2' col3 = 'c2' col4 = 'd2' )
( col1 = 'a3' col2 = 'b3' col3 = 'c3' col4 = 'd3' ) ).
* Actual logic
DATA target_line TYPE ty_target.
DATA target_tab1 TYPE tt_target.
LOOP AT employees INTO DATA(employee).
LOOP AT source_tab INTO DATA(source_line).
CLEAR target_line.
target_line-col4_vorna = employee-vorna.
target_line-col5_nachn = employee-nachn.
MOVE-CORRESPONDING source_line TO target_line.
APPEND target_line TO target_tab1.
ENDLOOP.
ENDLOOP.
cl_demo_output=>write( employees ).
cl_demo_output=>write( source_tab ).
cl_demo_output=>write( target_tab1 ).
cl_demo_output=>display( ).
I spend some time thinking, how I could improve the actual logic by using some “new” ABAP syntax elements. I came up with the following result.
DATA target_tab2 TYPE tt_target.
LOOP AT employees INTO employee.
target_tab2 = VALUE #( BASE CORRESPONDING #( target_tab2 )
FOR line IN source_tab (
VALUE #( BASE CORRESPONDING #( line )
col4_vorna = employee-vorna
col5_nachn = employee-nachn ) ) ).
ENDLOOP.
But I must say, that in this case I prefer using nested LOOP’s like this
DATA target_tab3 TYPE tt_target.
LOOP AT employees INTO employee.
LOOP AT source_tab INTO source_line.
DATA(t_line) = CORRESPONDING ty_target( source_line ).
t_line-col4_vorna = employee-vorna.
t_line-col5_nachn = employee-nachn.
APPEND t_line TO target_tab3.
ENDLOOP.
ENDLOOP.
or this
DATA target_tab4 TYPE tt_target.
LOOP AT employees INTO employee.
LOOP AT source_tab INTO source_line.
APPEND VALUE #( BASE CORRESPONDING #( source_line )
col4_vorna = employee-vorna
col5_nachn = employee-nachn ) TO target_tab4.
ENDLOOP.
ENDLOOP.