'Side by side structure values comparison and showing the difference?
How to compare structure values component-wise and display the differences?
Now I do it in a very primitive way:
DATA: tkomp TYPE komp,
tkomp2 TYPE komp.
WRITE: `Field differences: `.
DO 500 TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE tkomp TO FIELD-SYMBOL(<fld>).
IF sy-subrc = 0.
CHECK <fld> IS NOT INITIAL AND CONV string( <fld> ) CN ' 0,.'.
ENDIF.
ASSIGN COMPONENT sy-index OF STRUCTURE tkomp2 TO FIELD-SYMBOL(<fld2>).
IF sy-subrc <> 0.
EXIT.
ENDIF.
IF <fld> <> <fld2>.
WRITE: / `Component ` && sy-index && ` differs: ` && <fld>.
ENDIF.
ENDDO.
Maybe there is more beautiful way?
I found oldie threads, maybe there is something like CL_ABAP_CORRESPONDING or something newer?
BTW, in the above thread they say The Debugger uses the class CL_TPDA_TOOL_DIFF for analyzing differences, hence is my follow-ip question:
is it something that we can achieve in debugger? I've never seen an applet in ABAP debugger that allows comparing structures against each other.
Solution 1:[1]
Your solution is actually quite okay. You might want to add CL_ABAP_STRUCTDESCR to get the names of the components for nicer output. It would also enable you to compare and analyze the types of the component's fields as well.
Unfortunately, there is no reusable class, function, or built-in method for that.
You will find the most precise comparison implementation in the class CL_ABAP_UNIT_ASSERT, method ASSERT_EQUALS. More precisely, the local class DATA_DIFF, method DIFF_STRUCTS, shows how to compare structures in a type- and nesting-tolerant way.
I can't speak for the class CL_TPDA_TOOL_DIFF. I've heard it mentioned before but we actually do not even have it in our SAP NW 7.52 systems.
I have also never seen a debugger view or plugin that would compare structures and display the differences. No idea where that comment comes from.
Solution 2:[2]
There are code snippets at below, you can pass the structure in cl_abap_typedescr=>describe_by_data.
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
lt_comp TYPE abap_component_tab,
ls_comp TYPE abap_componentdescr.
CLEAR lo_struct.
lo_struct ?= cl_abap_typedescr=>describe_by_data( p_data = lt_list_sum ).
REFRESH lt_comp.
lt_comp = lo_struct->get_components( ).
MOVE-CORRESPONDING gt_mov_grp TO gt_mov_grp_std.
LOOP AT lt_comp INTO ls_comp.
CLEAR lv_numeric.
IF ls_comp-name(4) EQ cx_move.
lv_numeric = ls_comp-name+5(2).
READ TABLE gt_mov_grp_std REFERENCE INTO lr_mov_grp WITH KEY report_order = lv_numeric.
IF sy-subrc EQ 0 AND
lr_mov_grp IS BOUND.
APPEND INITIAL LINE TO et_data REFERENCE INTO DATA(lr_data).
MOVE-CORRESPONDING lr_mov_grp->* TO lr_data->*.
IF <fs> IS ASSIGNED.
UNASSIGN <fs>.
ENDIF.
ASSIGN COMPONENT ls_comp-name OF STRUCTURE lt_list_sum TO <fs>.
IF sy-subrc EQ 0 AND
<fs> IS ASSIGNED.
lr_data->amount = <fs>.
ENDIF.
IF <fs> IS ASSIGNED.
UNASSIGN <fs>.
ENDIF.
ASSIGN COMPONENT cx_currency OF STRUCTURE lt_list_sum TO <fs>.
IF sy-subrc EQ 0 AND
<fs> IS ASSIGNED.
lr_data->currency = <fs>.
ENDIF.
lr_data->datum = iv_datum.
lr_data->werks = iv_werks.
lr_data->kunnr = iv_kunnr.
ENDIF.
ENDIF.
ENDLOOP.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Florian |
| Solution 2 | Halil Ural |
