*&---------------------------------------------------------------------*
*& Report TASK #01
*&---------------------------------------------------------------------*
*& Description: 'Let's have some ABAP fun' contest - Task #1
* There are two internal tables declared in the program.
* On the selection screen, there is a parameter (character)
* where user can specify the name of the internal table that
* should be added with one line. So, the name of the table
* in account is stored in the variable in the runtime.
*
* How will you mangae adding the row?
*& Author : Miroslav Oprsteny
*& Date : 2011-10-14
*&---------------------------------------------------------------------*
report task_#01.
type-pools : abap.
types:
* row type of table A
begin of ls_line,
value type c,
end of ls_line,
*row type of table B
begin of ls_line2,
name type c,
value type c,
end of ls_line2.
data: lt_a type table of ls_line, " table A
lt_b type table of ls_line2, " table B
l_data type ref to data, " temp variable to retrieve the table structure
l_table_name(4) type c, " dynamic table name
idetails type abap_compdescr_tab, " dynamic table fields info
xdetails type abap_compdescr, " info of one field of dyn. table
ref_table_des type ref to cl_abap_structdescr. " class reference
field-symbols:
<fs_table_line>, " line of dynamic table
<fs_table> type standard table, " dynamic table
<comp>. " component used to write to structures
parameters p_table type c.
concatenate 'lt_' p_table into l_table_name. " contruct dynamic table name
assign (l_table_name) to <fs_table>. " pointer to selected table
create data l_data like line of <fs_table>. " created temp data
assign l_data->* to <fs_table_line>.
" Retrieve table structure info
ref_table_des ?= cl_abap_typedescr=>describe_by_data( <fs_table_line> ).
idetails[] = ref_table_des->components[].
" Add single line to dynamic table
append initial line to <fs_table> assigning <fs_table_line>.
" Loop over all fields of a dynamic table and assign values
loop at idetails into xdetails.
case xdetails-name.
when 'VALUE'.
" <fs_table_line> is type of ANY so we need to use COMPONENT approach
assign component 'VALUE' of structure <fs_table_line> to <comp> casting type c.
<comp> = 'X'.
when 'NAME'.
" this component is present only for table lt_b
assign component 'NAME' of structure <fs_table_line> to <comp> casting type c.
<comp> = 'Y'.
endcase.
endloop.
" Now write contents of both tables to output
write 'Loop at table A:'.
loop at lt_a assigning <fs_table_line>.
assign component 'VALUE' of structure <fs_table_line> to <comp>.
write / <comp>.
endloop.
write / 'Loop at table B:'.
loop at lt_b assigning <fs_table_line>.
assign component 'NAME' of structure <fs_table_line> to <comp>.
write / <comp>.
assign component 'VALUE' of structure <fs_table_line> to <comp>.
write: ':', <comp>.
endloop.
Share it with your friends:
Related