06-30-2020 3:53 PM
Hi Experts,
I am trying to solve the min coin change problem in SAP ABAP https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/ . I would have to create a matrix table dynamically. I have searched the SCN and found few posts, But everything is about creating a field catalog. Creating a matrix using "For" constructor creates a deep structure.
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencond_iteration_matrix_abexa.htm
Is there a simple way to create a dynamic matrix table in ABAP?
07-03-2020 11:48 PM
Hi,
As suggested I used "For" over "Value" to create a matrix to solve the problem. Used the sample code from
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencond_iteration_matrix_abexa.htm
06-30-2020 5:26 PM
Nope.
By the way, consider looking at the TDD openSAP course. I seem to recall this is one of the exercises.
06-30-2020 10:26 PM
And why exactly is a table type with a deep structure not "simple"?
You 'simply' access a matrix cell by its x and y values with MATRIX[x][y].
What am I missing?
06-30-2020 11:51 PM
I personally would prefer using a matrix with a deep structure (table in table), but you certainly can also create a table type with the amount of necessary columns dynamically.
But this would be a sample coding to do the job. It is probably not as 'simple' as you would have hoped for, but it still might be shortened in one way or the other. Both logics, matrix with table in table and table with struc are presented in this coding:
REPORT zdemo_matrix.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: main.
PRIVATE SECTION.
CLASS-DATA:
rows TYPE i,
columns TYPE i,
x TYPE i VALUE 1,
y TYPE i VALUE 1.
CLASS-METHODS initialize.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
initialize( ).
* create the matrix based on table in table
TYPES: t_column TYPE STANDARD TABLE OF string WITH EMPTY KEY,
t_rows TYPE STANDARD TABLE OF t_column WITH EMPTY KEY.
DATA(matrix_tab) = VALUE t_rows(
FOR i = 0 UNTIL i > columns - 1 (
VALUE t_column(
FOR j = 1 UNTIL j > rows
( sy-abcde+i(1) && |{ j }| ) ) ) ).
* matrix based on table in table created
* create the matrix based on table with structure
DATA abaptype TYPE REF TO cl_abap_datadescr.
DATA row TYPE int4.
DATA column TYPE int4.
DATA dref TYPE REF TO data.
FIELD-SYMBOLS <matrix_str> TYPE STANDARD TABLE.
abaptype ?= cl_abap_datadescr=>describe_by_name( 'STRING' ). " cell type definition
DATA(comp_tab) = VALUE abap_component_tab( FOR i = 0 UNTIL i >= columns " create row components based on cell
( name = sy-abcde+i(1) type = abaptype ) ). " opted for single letters as column name
" with current limit of 26 columns
DATA(structype) = cl_abap_structdescr=>create( comp_tab ). " create row structure
DATA(tabletype) = cl_abap_tabledescr=>create( " create table based on row structure
p_line_type = structype p_table_kind = 'S' ).
CREATE DATA dref TYPE HANDLE tabletype.
ASSIGN dref->* TO <matrix_str>. " the matrix table (without rows yet)
DO rows TIMES. " creating rows for matrix with columns
row = row + 1.
APPEND INITIAL LINE TO <matrix_str> ASSIGNING FIELD-SYMBOL(<line>).
DO columns TIMES. " creating inital xy-values
column = sy-index - 1.
ASSIGN COMPONENT sy-index OF STRUCTURE <line> TO FIELD-SYMBOL(<fs>).
<fs> = sy-abcde+column(1) && |{ row }|.
ENDDO.
ENDDO.
* matrix based on table with structure created
* Access & display matrix as table in table
ASSIGN matrix_tab[ x ][ y ] TO <fs>. " access matrix-cell of table in table
cl_demo_output=>write_data( <fs> ).
cl_demo_output=>write_data( matrix_tab ).
* Access & display matrix as table with struc
ASSIGN COMPONENT y OF STRUCTURE <matrix_str>[ x ] TO <fs>. " access matrix-cell of table with structure
cl_demo_output=>write_data( <fs> ).
cl_demo_output=>write_data( <matrix_str> ).
cl_demo_output=>display( ).
ENDMETHOD.
METHOD initialize.
rows = 100.
columns = strlen( sy-abcde ).
cl_demo_input=>add_field( CHANGING field = x ).
cl_demo_input=>add_field( CHANGING field = y ).
cl_demo_input=>request( ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
07-01-2020 7:05 AM
You say "Creating a matrix using "For" constructor creates a deep structure."
If "deep structure" is the official ABAP term (see Glossary - Deep), you're wrong. It may create a "flat" or "deep" structure i.e. any kind of structure...
What can be said, but it has no importance concerning your question, is that "For" creates a structure based on a standalone and complete/non-generic data type (see ABAP documentations Glossary - Standalone data type and Glossary - complete data type).
What you are looking for is Run Time Type Creation (see ABAP documentation Glossary - Run Time Type Creation).
07-01-2020 11:01 PM
Hi,
Thanks for explaining the differences and that "For" constructor is easier to use than actually creating Run time type creation. "For" matrix creation is clearly easily accessible.
07-03-2020 11:48 PM
Hi,
As suggested I used "For" over "Value" to create a matrix to solve the problem. Used the sample code from
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencond_iteration_matrix_abexa.htm
07-04-2020 2:25 PM
Additional remarks about the wording. As I re-read my last comment, ""For" constructor creates a [...] structure" shocked me so I want to clarify my last comment.
"For" is not a constructor (operator?), only "value", "reduce" and so on are constructor operators. Eventually "for" is a possible element of a constructor expression. It's more precise to say that "For" is a loop over an internal table(s) to append or initialize another internal table or to reduce it to any kind of data object.