Code: Alles auswählen.
REPORT zknobelufgabe_2021_10.
CLASS lcl_knobel DEFINITION FINAL.
PUBLIC SECTION.
TYPES: BEGIN OF mts_data,
field1 TYPE text40,
field2 TYPE text40,
field3 TYPE text40,
field4 TYPE text40,
END OF mts_data,
mtt_data TYPE STANDARD TABLE OF mts_data WITH NON-UNIQUE DEFAULT KEY,
mtt_spool TYPE STANDARD TABLE OF text1024 WITH NON-UNIQUE DEFAULT KEY.
METHODS:
main,
build_testdata RETURNING VALUE(rt_data) TYPE mtt_data,
build_spool IMPORTING it_data TYPE mtt_data
RETURNING VALUE(rt_spool) TYPE mtt_spool,
analyze_spool IMPORTING it_spool TYPE mtt_spool
RETURNING VALUE(rt_data) TYPE mtt_data.
ENDCLASS.
PARAMETERS: simple AS CHECKBOX DEFAULT 'X'.
END-OF-SELECTION.
NEW lcl_knobel( )->main( ).
CLASS lcl_knobel IMPLEMENTATION.
METHOD main.
DATA: lv_color TYPE i.
DATA(lt_original_data) = me->build_testdata( ).
DATA(lt_spool) = me->build_spool( lt_original_data ).
DATA(lt_spool_analysis) = me->analyze_spool( lt_spool ).
IF lines( lt_original_data ) <> lines( lt_spool_analysis ).
WRITE 'Originaldatei und Analyse haben unterschiedliche Anzahl von Zeilen' COLOR 6.
ELSEIF lt_original_data = lt_spool_analysis.
WRITE 'Das ist korrekt' COLOR 5.
ELSE.
WRITE 'Leider noch nicht korrekt' COLOR 6.
ENDIF.
SKIP 2.
LOOP AT lt_spool ASSIGNING FIELD-SYMBOL(<lv_spool>).
DATA(lv_tabix) = sy-tabix - 3.
READ TABLE lt_original_data INDEX lv_tabix ASSIGNING FIELD-SYMBOL(<ls_original>).
IF sy-subrc <> 0. " Mein Fehler oder Anfang oder Ende der LIste
lv_color = 0.
ELSE.
READ TABLE lt_spool_analysis INDEX lv_tabix ASSIGNING FIELD-SYMBOL(<ls_spool_analysis>).
IF sy-subrc <> 0
OR <ls_spool_analysis> <> <ls_original>.
lv_color = 7. " falsch
ELSE.
lv_color = 5. " passt
ENDIF.
ENDIF.
DATA(lv_len) = strlen( <lv_spool> ).
WRITE: AT /(lv_len) <lv_spool> COLOR = lv_color.
ENDLOOP.
ENDMETHOD.
METHOD analyze_spool.
**** Hier eure Analyse!
* Leider nicht so tolle Lösung, da die Prüfung ergibt, dass dies falsch sei.
LOOP AT it_spool ASSIGNING FIELD-SYMBOL(<lv_spool>) FROM 4.
APPEND INITIAL LINE TO rt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
SPLIT <lv_spool> AT sy-vline INTO DATA(lv_dummy) <ls_data>-field1 <ls_data>-field2 <ls_data>-field3 <ls_data>-field4.
ENDLOOP.
ENDMETHOD.
METHOD build_spool.
DATA: ls_data LIKE LINE OF it_data,
lv_length TYPE i.
DATA(lo_csv) = cl_rsda_csv_converter=>create( i_delimiter = '"' i_separator = cl_abap_char_utilities=>horizontal_tab ).
LOOP AT it_data ASSIGNING FIELD-SYMBOL(<ls_data>).
APPEND INITIAL LINE TO rt_spool ASSIGNING FIELD-SYMBOL(<lv_spool>).
lo_csv->structure_to_csv( EXPORTING i_s_data = <ls_data> IMPORTING e_data = <lv_spool> ).
<lv_spool> = |{ cl_abap_char_utilities=>horizontal_tab }{ <lv_spool> }{ cl_abap_char_utilities=>horizontal_tab }|. " Anfang und Ende auch
ENDLOOP.
* Überschrift hinzufügen, damit es wie ein spool aussieht
lv_length = strlen( <lv_spool> ).
INSERT sy-uline(lv_length) INTO rt_spool INDEX 1.
INSERT sy-uline(lv_length) INTO rt_spool INDEX 1.
APPEND sy-uline(lv_length) TO rt_spool.
lv_length = strlen( <ls_data>-field1 ).
ls_data-field1 = 'FIELD1----------------------------------'.
ls_data-field1 = ls_data-field1(lv_length).
lv_length = strlen( <ls_data>-field2 ).
ls_data-field2 = 'FIELD2----------------------------------'.
ls_data-field2 = ls_data-field2(lv_length).
lv_length = strlen( <ls_data>-field3 ).
ls_data-field3 = 'FIELD3----------------------------------'.
ls_data-field3 = ls_data-field3(lv_length).
lv_length = strlen( <ls_data>-field4 ).
ls_data-field4 = 'FIELD4----------------------------------'.
ls_data-field4 = ls_data-field4(lv_length).
INSERT INITIAL LINE INTO rt_spool INDEX 2 ASSIGNING FIELD-SYMBOL(<lv_header>).
lo_csv->structure_to_csv( EXPORTING i_s_data = ls_data IMPORTING e_data = <lv_header> ).
<lv_header> = |{ cl_abap_char_utilities=>horizontal_tab }{ <lv_header> }{ cl_abap_char_utilities=>horizontal_tab }|. " Anfang und Ende auch
REPLACE ALL OCCURRENCES OF '-' IN <lv_header> WITH ` `.
* Aufhübschen, damit es wie eine ALV-Liste aussieht
REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN TABLE rt_spool WITH sy-vline.
ENDMETHOD.
METHOD build_testdata.
DATA: lo_rnd TYPE REF TO cl_abap_random_int,
lo_char TYPE REF TO cl_abap_random_int.
lo_rnd = cl_abap_random_int=>create( seed = CONV #( sy-uzeit ) min = 10 max = 40 ).
IF simple = 'X'.
lo_char = cl_abap_random_int=>create( seed = CONV #( sy-uzeit ) min = 65 max = 90 ). " Usual ASCII characters after " ( to avoid csv-shenanigans )
ELSE.
lo_char = cl_abap_random_int=>create( seed = CONV #( sy-uzeit ) min = 35 max = 126 ). " Usual ASCII characters after " ( to avoid csv-shenanigans )
ENDIF.
* 100 Testreihen
DO 100 TIMES.
APPEND INITIAL LINE TO rt_data.
ENDDO.
DO 4 TIMES.
DATA(lv_index) = sy-index.
DATA(lv_length) = lo_rnd->get_next( ).
LOOP AT rt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
DO lv_length TIMES.
ASSIGN COMPONENT lv_index OF STRUCTURE <ls_data> TO FIELD-SYMBOL(<lv_field>).
<lv_field> = |{ <lv_field> }{ cl_abap_conv_in_ce=>uccpi( lo_char->get_next( ) ) }|.
ENDDO.
ENDLOOP.
ENDDO.
ENDMETHOD.
ENDCLASS.
Folgende Benutzer bedankten sich beim Autor black_adept für den Beitrag:
a-dead-trousers
Du hast zumindest den Knackpunkt entdeckt. 😛
Code: Alles auswählen.
FIND ALL OCCURRENCES OF REGEX '[^|]+' IN <ld_spool> IN CHARACTER MODE RESULTS lt_header.
Code: Alles auswählen.
pro Listzeile <ld_spool>:
LOOP AT lt_header ASSIGNING FIELD-SYMBOL(<ls_header>).
ASSIGN COMPONENT sy-tabix OF STRUCTURE <ls_data> TO FIELD-SYMBOL(<lv_datafield>).
ASSIGN <ld_spool>+<ls_header>-offset(<ls_header>-length) TO <ld_field>.
<lv_datafield> = <ld_field>.
ENDLOOP.
Code: Alles auswählen.
*lv_real_separators vorher:
*|FIELD1 |FIELD2 |FIELD3 |FIELD4 |
REPLACE ALL OCCURRENCES OF REGEX '[^\|]' IN lv_real_separators WITH ` `.
*lv_real_separators nachher:"
*| | | | |
REPLACE ALL OCCURRENCES OF '|' IN lv_real_separators WITH cl_abap_char_utilities=>horizontal_tab.
Code: Alles auswählen.
pro Listzeile <lv_spool>:
DATA(lv_spool) = lv_real_separators. "Kopie der Tabulator-Trennzeichenzeile erhalten
OVERLAY lv_spool WITH <lv_spool>. " Kominieren der Spoolzeile mit den Tabulatoren
SPLIT lv_spool AT cl_abap_char_utilities=>horizontal_tab INTO DATA(lv_dummy) <ls_data>-field1 <ls_data>-field2 <ls_data>-field3 <ls_data>-field4 DATA(lv_dummy2).
Folgende Benutzer bedankten sich beim Autor black_adept für den Beitrag:
a-dead-trousers