Tips and Tricks for the ABAP® Programming Language

Y9030014: Determine the header and the data of an internal table automatically.

The printout of an internal table is often connected with counting out the headings and/or the data fields for output. Here some general routines process the content of an internal table, create the heading line and set the splitting precisely for the respective data. The routines should by generally suitable for other internal tables (with a maximum length of 256 Bytes). The gathering of the field names of an internal table can occur from the memory or directly from the current report.

We chosen the second way. In the shortness of time, it was not possible for us to fully test this report. Please send us a brief email if any “diffuseness” occurs. Thank you.

ABAP-Source-Code

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

REPORT Y9030014 LINE-SIZE 250.    "Release 3.1G, 4.5A
************************************************************************
* Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com
* 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.
************************************************************************
* Important: - The definition of the internal table must be directly
*            - Comments in the definition are allowed
*            - This report does not allow definition with "structure xx"
************************************************************************
*BREAK-POINT.
*//////////////////////////////////////////////////////////////////////*
 WRITE: 'Work with an internal table and WRITE variable headline, data'
          TO SY-TITLE.
*//////////////////////////////////////////////////////////////////////*
***************         Parameters                **********************
  PARAMETERS: P_PTEST(1) TYPE C DEFAULT 'J'.    "output fieldnames
*
***************         External tables           **********************
 TABLES: YT100.                         "only use for CT-DEBUG_Simulator
*
***************         Variables                 **********************
 DATA: OFF TYPE I.
 DATA: WORK(256) TYPE C.
 DATA: ANZ TYPE I.
 DATA: LEN1 TYPE I.
*
***************         internal tables           **********************
* ................. your internal table ................................
 DATA: BEGIN OF IT100 OCCURS 200,
          ZNUMBER1(6) TYPE N,                   "not append in struct.
          ZLAENG1(05) TYPE N,                   " this also
*
          ZSPRSL(1) TYPE C,
          ZARBGB(20) TYPE C,
          ZMSGNR(03) TYPE C,
          ZTEXT1(63) TYPE C,
          ZTEXT2(63) TYPE C,
       END OF IT100.
*
 DATA: BEGIN OF ITREP OCCURS 500,                        "Report source
         F1(72) TYPE C,
       END OF ITREP.
*
 DATA: BEGIN OF I_FNAMES OCCURS 50,
         F1(72) TYPE C,
       END OF I_FNAMES.
*
***************         Field symbols             **********************
FIELD-SYMBOLS: <FS1>, <FS2>.
*//////////////////////////////////////////////////////////////////////*
*************               Main Section             *******************
*//////////////////////////////////////////////////////////////////////*
*
 PERFORM READ-YT100.

 PERFORM DISPLAY-HEAD-DATA TABLES IT100 USING 'IT100'.

*                                 |-> ITAB      |
*                                               |-> itab name
*
*//////////////////////////////////////////////////////////////////////*
*************                Subroutines             *******************
*//////////////////////////////////////////////////////////////////////*
************************************************************************
*                  Read and display a few rows of DD01L
************************************************************************
 FORM READ-YT100.
*
  ULINE. SKIP 2.
  WRITE: /5 'Step 1: Read YT100 and fill IT100   ' COLOR 5.
  SKIP 2.
*.......................................................................
  DATA: ZLEN1 TYPE I.
*.......................................................................
  SELECT * FROM YT100
    UP TO 40 ROWS
    WHERE ZSPRSL EQ 'E'.
*
    MOVE-CORRESPONDING YT100 TO IT100.
    MOVE SY-DBCNT TO IT100-ZNUMBER1.
    ZLEN1 = STRLEN( YT100-ZTEXT1 ).
    IF SY-SUBRC EQ 0.
       IT100-ZLAENG1 = ZLEN1.
    ELSE.
       WRITE: /1 'Error= STRLEN', ZLEN1, YT100 COLOR 5.
    ENDIF.
*
    APPEND IT100.
*
  ENDSELECT.
*
 ENDFORM.
************************************************************************
*                    Display header and data
************************************************************************
 FORM DISPLAY-HEAD-DATA TABLES ITAB STRUCTURE IT100 USING TABL1.
*
  ULINE. SKIP 2.
  WRITE: /5 'Step 2: Display the headline and data of IT100  ' COLOR 5.
  WRITE: /5 ' this can also be used with other internal tables' COLOR 7.
  SKIP 2.
*.................Information about field names of internal tables......
    PERFORM READ-FIELDNAMES USING TABL1.
*................. here the headline of (any) internal table ...........
    LOOP AT ITAB.
      IF SY-TABIX EQ 1.
         DO 200 TIMES.                                   "max 200 fields
            ASSIGN COMPONENT SY-INDEX OF STRUCTURE ITAB TO <FS1>.
            IF SY-SUBRC NE 0.
               EXIT.
            ENDIF.
            DESCRIBE FIELD <FS1> LENGTH LEN1.
            READ TABLE I_FNAMES INDEX SY-INDEX.
            IF SY-SUBRC EQ 0.
               IF SY-INDEX EQ 1.
                  MOVE '|' TO WORK+OFF(1).
                  ADD 1 TO OFF.
               ENDIF.
               MOVE I_FNAMES TO WORK+OFF(LEN1).
               ADD LEN1 TO OFF.
               MOVE  '|' TO WORK+OFF(1).
               ADD 1 TO OFF.
            ELSE.
               MOVE 'NN' TO WORK+OFF(LEN1).
               ADD LEN1 TO OFF.
               MOVE  '|' TO WORK+OFF(1).
               ADD 1 TO OFF.
            ENDIF.
         ENDDO.
         ULINE.
         WRITE: /1 WORK COLOR 3.
         ULINE.
         CLEAR: WORK, LEN1, OFF.
      ENDIF.
    ENDLOOP.
*
***************  here the data of (any) internal table *****************
*
    LOOP AT ITAB.
      DO 200 TIMES.                                      "max 200 fields
         ASSIGN COMPONENT SY-INDEX OF STRUCTURE ITAB TO <FS1>.
         IF SY-SUBRC NE 0.
            EXIT.
         ENDIF.
         DESCRIBE FIELD <FS1> LENGTH LEN1.
         IF SY-INDEX EQ 1.
            MOVE '|' TO WORK+OFF(1).
            ADD 1 TO OFF.
         ENDIF.
         MOVE <FS1> TO WORK+OFF(LEN1).
         ADD LEN1 TO OFF.
         MOVE  '|' TO WORK+OFF(1).
         ADD 1 TO OFF.
      ENDDO.
         WRITE: /1 WORK.
         CLEAR: WORK, LEN1, OFF.
    ENDLOOP.
*
 ENDFORM.
************************************************************************
*                 Read fieldnames of internal table
************************************************************************
 FORM READ-FIELDNAMES USING TABL1.
* One probably can read out the desired information directly from the
* memory or other sources. We choosed a more official way by reading
* the report source with the command READ REPORT ...
*.......................................................................
 DATA: FIELDS1 TYPE I.
*.......................................................................
   READ REPORT 'Y9030014' INTO ITREP.         "only for debug-simulation
*  READ REPORT SY-REPID INTO ITREP.           "... in the real world
   IF SY-SUBRC EQ 0.
     LOOP AT ITREP.
        IF ITREP-F1 CS 'END OF'.
           EXIT.
        ENDIF.
        IF ITREP-F1 CS 'OCCURS'.
           IF ITREP-F1 CS TABL1.
              MOVE 1 TO FIELDS1.
              CONTINUE.
           ENDIF.
        ENDIF.
        IF FIELDS1 EQ 1.
           IF ITREP-F1+0(1) EQ '*'.
              CONTINUE.
           ENDIF.
           MOVE ITREP-F1 TO I_FNAMES.
           CONDENSE I_FNAMES.
           APPEND I_FNAMES.
        ENDIF.
     ENDLOOP.
   ELSE.
      WRITE: /1 'No headline possible'.
   ENDIF.
*.......................................................................
  IF P_PTEST EQ 'J'.
     WRITE: /1 'The fieldnames of the internal table:' COLOR 3, TABL1.
     SKIP. ULINE.
     LOOP AT I_FNAMES.
         WRITE: /1 I_FNAMES.
     ENDLOOP.
     SKIP 2. ULINE.
*
  ENDIF.
*
 ENDFORM.
************************************************************************
************************************************************************
******************* END OF PROGRAM *************************************