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: 

Min Coin Change

0 Kudos

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

https://blogs.sap.com/2013/06/11/dynamic-internal-table-iilustrated-with-an-example-of-creating-the-...

Is there a simple way to create a dynamic matrix table in ABAP?

1 ACCEPTED SOLUTION

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

7 REPLIES 7

matt
Active Contributor
0 Kudos

Nope.

By the way, consider looking at the TDD openSAP course. I seem to recall this is one of the exercises.

michael_piesche
Active Contributor

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?

michael_piesche
Active Contributor

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.

  • First of all you have to give your columns field names and they cant start with a number so either a letter or underscore '_x' would be an option.
  • And especially accessing this table is not as neat:
    ASSIGN COMPONENT y OF STRUCTURE <table>[ x ] TO <fs>.
    vs.
    ASSIGN <table>[ x ][ y ] TO <fs>.
  • Also, the matrix with a table in table can be 'easily' enlarged or shortened, whereas the matrix with a fixed structure needs to be recreated
  • If you have hoped for something other than a solution of a table with fixed fields or a table in table, I currently have to disappoint you, but I am also not sure what exactly you are looking for instead.

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( ).

Sandra_Rossi
Active Contributor

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).

0 Kudos

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.

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

Sandra_Rossi
Active Contributor

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.