cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Need help to optimize/compact code with abap 7.4 syntax

Angelo_Ab14
Participant
0 Kudos
619

Hi,

I have this code(optimized for copy/paste):

 

 

 

    TYPES: BEGIN OF ty_ctr,
             doc_num TYPE string,
             doc_soc TYPE string,
             doc_typ TYPE string,
           END OF ty_ctr.

    TYPES tty_ctr TYPE STANDARD TABLE OF ty_ctr WITH DEFAULT KEY.

    CONSTANTS c_aq      TYPE string VALUE 'AQ'.
    CONSTANTS c_def     TYPE string VALUE 'ZZZZ'.

    DATA lt_ctr_temp2   TYPE tty_ctr.

    DATA(lt_ctr_ret) = VALUE tty_ctr( ( doc_num = '123' doc_soc = 'ZZ01' doc_typ = 'AQ' )
                                      ( doc_num = '123' doc_soc = 'ZZ02' doc_typ = 'AQ' )
                                      ( doc_num = '234' doc_soc = 'ZZ03' doc_typ = 'AQ' )
                                      ( doc_num = '567' doc_soc = 'ZZ04' doc_typ = 'ZZ' ) ).

    LOOP AT lt_ctr_ret INTO DATA(ls_ctr_ret).
      DATA(lt_ctr_temp1) =  VALUE tty_ctr( FOR ls_ctr IN lt_ctr_ret WHERE ( doc_num = ls_ctr_ret-doc_num AND doc_typ = c_aq ) ( ls_ctr ) ).
      DELETE lt_ctr_ret WHERE doc_num = ls_ctr_ret-doc_num AND doc_typ = c_aq.
      IF lines( lt_ctr_temp1 ) > 1.
        SORT lt_ctr_temp1 BY doc_num.
        DELETE ADJACENT DUPLICATES FROM lt_ctr_temp1 COMPARING doc_num.
        DATA(ls_ctr_soc) = VALUE ty_ctr( doc_soc = c_def ).
        MODIFY lt_ctr_temp1 FROM ls_ctr_soc TRANSPORTING doc_soc WHERE doc_num EQ ls_ctr_ret-doc_num.
      ENDIF.
      APPEND LINES OF lt_ctr_temp1 TO lt_ctr_temp2.
    ENDLOOP.
    APPEND LINES OF lt_ctr_temp2 TO lt_ctr_ret.

 

 

 

This code simply search for document number with doc type 'AQ'.

If there are 'AQ' documents with multiple society the result is a single row with society 'ZZZZ'.

lt_ctr_ret is the input and result table.

Input:

Angelo_Ab_1-1741342477918.png

Result:

Angelo_Ab_2-1741342519404.png

I need to know if there is a way tu compact and optimize this code with abap 7.4/7.5x syntax.

Thank you.

Accepted Solutions (0)

Answers (1)

Answers (1)

kiran_angar
Explorer
TYPES: BEGIN OF ty_ctr,
         doc_num TYPE string,
         doc_soc TYPE string,
         doc_typ TYPE string,
       END OF ty_ctr.

TYPES tty_ctr TYPE SORTED TABLE OF ty_ctr WITH NON-UNIQUE KEY doc_num doc_typ.

CONSTANTS c_def     TYPE string VALUE 'ZZZZ'.

DATA lt_ctr_temp2   TYPE tty_ctr.

DATA(lt_ctr_ret) = VALUE tty_ctr( ( doc_num = '123' doc_soc = 'ZZ01' doc_typ = 'AQ' )
                                  ( doc_num = '123' doc_soc = 'ZZ02' doc_typ = 'AQ' )
                                  ( doc_num = '234' doc_soc = 'ZZ03' doc_typ = 'AQ' )
                                  ( doc_num = '567' doc_soc = 'ZZ04' doc_typ = 'ZZ' ) ).

LOOP AT lt_ctr_ret ASSIGNING FIELD-SYMBOL(<lfs_ctr_ret>)
                                              GROUP BY ( item = <lfs_ctr_ret>-doc_num
                                                         doc_typ = <lfs_ctr_ret>-doc_typ
                                                         size = GROUP SIZE ) ASCENDING
                                   REFERENCE INTO DATA(lv_ctr_ret_group).



  LOOP AT GROUP lv_ctr_ret_group ASSIGNING FIELD-SYMBOL(<lfs_grp_members>) ."WHERE posnr = <xvbap>-posnr." Loop through each group
    IF lv_ctr_ret_group->size > 1.
      <lfs_grp_members>-doc_soc = c_def.
      APPEND <lfs_grp_members> TO lt_ctr_temp2.
      EXIT. " Exit after 1 run
    ELSE.
      APPEND <lfs_grp_members> TO lt_ctr_temp2.
    ENDIF.
  ENDLOOP.
ENDLOOP.

 cl_demo_output=>display_data( lt_ctr_temp2 ).

Hello,

I think this could be a simpler version of the code.

Thanks
Kiran Angar

Angelo_Ab14
Participant
0 Kudos
Nice one!
Sandra_Rossi
Active Contributor
0 Kudos

For better legibility, don't mix ASSIGNING and REFERENCE INTO if possible (it's possible here), keep names consistent ("item =" -> "doc_num =").

Also, it's best to give clues (e.g. did you try LOOP AT GROUP BY or GROUP BY in FOR iterations) than giving one solution to an exam without explanation, the OP won't have a good level if not doing efforts.