abap

Compare row to column between tables


I got a question: is it possible to compare rows with columns in ABAP? If so, how to achieve that?

Imagine we have a table like this one, where column has a number inside its name:

FNCURR01 FNCURR02 FNCURR03 FNCURR04
HWBAS HWSTE HWBAS HWBAS
HWBAS HWSTE HWBAS HWBAS

And we got another table which looks like this:

layout type column number text of column to add to another internal table
JC_REP03 S 1 Tax Amount 19%
JC_REP03 P 2 Tax Amount 19%
JC_REP03 P 3 Tax Amount 9%
JC_REP03 S 4 Tax Amount 23%

I want to get the values from column FNCURRXX, where XX it's a value of an row of column: "column number" of another table.

For example: FNCURR01 matches the number 1 in my second table, so i get the value HWBAS.

Is it possible to achieve that dynamically? What I meant by that is that I do not want to create another table and map it somehow by hand in se11.


Solution

  • You can use field symbol and assign field dynamicly.

    FIELD-SYMBOLS: <lv_value> TYPE any.
        
    READ TABLE table1 INDEX 1.
    LOOP AT table2.
      ASSIGN COMPONENT |FNCURR0{ table2-columnnumber }| OF STRUCTURE table1 TO <lv_value>.
      WRITE <lv_value>.
    ENDLOOP.
    

    If you want to use first table as master you can use below example:

    DATA: lo_descr TYPE REF TO cl_abap_structdescr,
          ls_comp  TYPE abap_compdescr,
          lv_name  TYPE abap_compname,
          lv_i     TYPE i.
    
    lo_descr ?= cl_abap_typedescr=>describe_by_data( table1 ).
    
    LOOP AT lo_descr->components INTO ls_comp.
      lv_name = ls_comp-name.
      REPLACE 'FNCURR' WITH '' INTO lv_name.
      lv_i = lv_name.
      READ TABLE table2 WITH KEY columnnumber = lv_i.
      CHECK sy-subrc IS INITIAL.
      WRITE:/ table2-columnnumber.
    ENDLOOP.