Tips and Tricks for the ABAP® Programming Language

Y9030019 – Helper macro for binary search demo

This report reads a text table. The columns 1-8 of each row will be used to create an internal table with a binary key. ZSUMME contains the count of idential keys.

ABAP-Source-Code

You can copy and paste the source code directly into the ABAP-Workbench.

REPORT Y9030019 LINE-SIZE 130.    "Release 3.1G, 4.5A
************************************************************************
* Copyright (c) 1999 by CT-Team, 33415 Verl
* only use for CT-DEBUG_Simulator
* you can use or modify this report for your own work as long
* as you don't try to sell or republish it.
* in no event will the author be liable for indirect, special,
* Incidental, or consequental damages (if any) arising out of
* the use of this report.
*
************************************************************************
 BREAK-POINT.
*//////////////////////////////////////////////////////////////////////*
 MOVE: 'Read tables and use a VARTAB (DEFINE-Macro)'
        TO SY-TITLE.
*//////////////////////////////////////////////////////////////////////*
***************         Externe Tabellen          **********************
 TABLES: YT100.                         "only use for CT-DEBUG_Simulator
*
 DATA: HUGO(13) TYPE C.
***************         Makrodefinitionen         **********************
DEFINE VARTAB.
  DATA: BEGIN OF &1 OCCURS 0.
     INCLUDE STRUCTURE &2.
     DATA ZSUMME1(5) TYPE N.
  DATA: END OF &1.
 END-OF-DEFINITION.
*
 DEFINE FILLTAB_BINARY.
   &1 = YT100.
   TRANSLATE &1 TO UPPER CASE.
   READ TABLE &1 WITH KEY (HUGO) = &2+0(8) BINARY SEARCH.
   CASE SY-SUBRC.
        WHEN 0.
           ADD 1 TO &1-ZSUMME1.
           MODIFY &1 INDEX SY-TABIX.
        WHEN 4.
           ADD 1 TO &1-ZSUMME1.
           INSERT &1 INDEX SY-TABIX.
        WHEN 8.
           ADD 1 TO &1-ZSUMME1.
           APPEND &1.
   ENDCASE.
 END-OF-DEFINITION.
*
*//////////////////////////////////////////////////////////////////////*
*************            Programmsteuerung           *******************
*//////////////////////////////////////////////////////////////////////*
*
*
 VARTAB IT100  YT100.                               "macro: global table
 PERFORM READ-YT100.
*
 PERFORM DISPLAY-IT100.
*
*//////////////////////////////////////////////////////////////////////*
*************              Unterprogramme            *******************
*//////////////////////////////////////////////////////////////////////*
************************************************************************
*           Read and display a few rows of YT100
************************************************************************
 FORM READ-YT100.
*
  ULINE. SKIP 2.
  WRITE: /5 'Step 1: Read YT100 and fill IT100 in binary mode' COLOR 5.
*.......................................................................
  SELECT * FROM YT100
    UP TO  70 ROWS
    WHERE ZSPRSL EQ 'E'
     AND  ZARBGB GT 'Z20'.
*
     MOVE 'ZTEXT1+0(8)' TO HUGO.
     FILLTAB_BINARY   IT100  IT100-ZTEXT1.                        "marco
*
  ENDSELECT.
*
 ENDFORM.
************************************************************************
*                    display internal table it100
************************************************************************
 FORM DISPLAY-IT100.
*
  ULINE. SKIP 2.
  WRITE: /5 'Step 2: display it100, binary-key are byte 1-8 of ZTEXT1'.
  SKIP 1.
*.......................................................................
    WRITE: /1 'ZSPRSL',
            8 'ZARBGB',
           15 'ZMSGNR',
           22 'ZSUMME',
           29 'ZTEXT1'.
    ULINE. SKIP.
*
   LOOP AT IT100.
      WRITE: /1 IT100-ZSPRSL,
              8 IT100-ZARBGB,
             15 IT100-ZMSGNR,
             22 IT100-ZSUMME1,
             29 IT100-ZTEXT1,
             29 IT100-ZTEXT1+0(8) COLOR 3.
   ENDLOOP.
*
 ENDFORM.
************************************************************************
************************************************************************
******************* Programmende ***************************************