Tips and Tricks for the ABAP® Programming Language
Y9030012: A fast alternative to “COLLECT itab”
The testing data for an internal table is created by a random number generator (see Y9030010). In contrast to COLLECT itab, the number of the table entries is not limited. The construction of the internal table is carried out in a sorted manner and in the case of a double key the processed values are summed up .
The line number depends on the data volumes and the keywords. If the keyword is available, the generated value will be summed to the positions.
– Insert keyword: The generated value will be transferred
– Append keyword: The generated value will be also transferred.
Parameter entries:
NUMBER 100: With this parameter entry, the number DO loop runs is determined to generate random numbers . The random number generator of “Y9030010” can be used relatively simply in other, own reports.
ABAP™-Source-Code
You can cut and paste the source code directly into the ABAP™-Workbench.
REPORT Y9030012 LINE-SIZE 130. "Release 3.1G, 4.5A ************************************************************************ * Copyright (c) 1999 by CT-Team, 33415 Verl, http://www.ct-software.com * * 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: 'Alternative to ''COLLECT itab'' for table construction ' TO SY-TITLE. *//////////////////////////////////////////////////////////////////////* PARAMETERS: ANZAHL(3) TYPE N DEFAULT ' 15'. *************** Internal tables ********************** DATA: BEGIN OF ITAB OCCURS 100, CFELD4(4) TYPE C, IFELD TYPE I, PFELD3(3) TYPE P, PFELD5(5) TYPE P, ANZAHL(3) TYPE P, END OF ITAB. * *************** Field strings ********************** DATA: BEGIN OF ITAB1, CFELD4(4) TYPE C, IFELD TYPE I, PFELD3(3) TYPE P, PFELD5(5) TYPE P, END OF ITAB1. ************************************************************************ * *//////////////////////////////////////////////////////////////////////* ************* Main Section ******************* *//////////////////////////////////////////////////////////////////////* * *....................................................................... END-OF-SELECTION. PERFORM GENERIEREN-DATEN-ITAB1. PERFORM DISPLAY-ITAB. * *//////////////////////////////////////////////////////////////////////* ************* Subroutines ******************* *//////////////////////////////////////////////////////////////////////* * ************************************************************************ * Generate random numbers for the table head line ************************************************************************ FORM GENERIEREN-DATEN-ITAB1. * WRITE:/1 'Step 1: Create the head line data for ITAB1' COLOR 5. SKIP 2. ULINE. *.......Fill head line of ITAB1 with random numbers .................... IF ANZAHL EQ SPACE. MOVE '10' TO ANZAHL. WRITE: /1 'Parameter entry was set to ''10'' '. ENDIF. *....................................................................... DO ANZAHL TIMES. * PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-CFELD4 4 'A'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-IFELD 3 'N'. "max 9 PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-PFELD3 2 'N'. PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-PFELD5 4 'N'. MOVE 'ABC' TO ITAB1-CFELD4+0(3). * PERFORM AUFBAU-TABELLE-ITAB. CLEAR ITAB. ENDDO. * ENDFORM. ************************************************************************ * The internal table ITAB is build with COLLECT and INSERT * ************************************************************************ FORM AUFBAU-TABELLE-ITAB. * IF SY-INDEX EQ 1. "from last do loop WRITE:/1 'Step 2: Create the table ITAB ' COLOR 3. SKIP 2. ULINE. ENDIF. *....................................................................... * READ TABLE ITAB WITH KEY ITAB1-CFELD4 BINARY SEARCH. * see also .... TRANSPORTING NO FIELDS CASE SY-SUBRC. WHEN 0. WRITE: /1 'Prepare key, only addition: '. PERFORM DISPLAY-KOPFZEILE-ITAB1. *....................................................................... ADD ITAB1-IFELD TO ITAB-IFELD. ADD ITAB1-PFELD3 TO ITAB-PFELD3. ADD ITAB1-PFELD5 TO ITAB-PFELD5. ADD 1 TO ITAB-ANZAHL. MODIFY ITAB INDEX SY-TABIX. "-------> MODIFY * WHEN 4. WRITE: /1 'Key will be inserted : '. PERFORM DISPLAY-KOPFZEILE-ITAB1. MOVE-CORRESPONDING ITAB1 TO ITAB. ADD 1 TO ITAB-ANZAHL. INSERT ITAB INDEX SY-TABIX. "-------> INSERT * WHEN 8. WRITE: /1 'Key will be append : '. PERFORM DISPLAY-KOPFZEILE-ITAB1. MOVE-CORRESPONDING ITAB1 TO ITAB. ADD 1 TO ITAB-ANZAHL. APPEND ITAB. "-------> APPEND * WHEN OTHERS. WRITE: /1 'Surprise-RC-Code in '' AUFBAU-ITAB'' ', SY-SUBRC. ENDCASE. * ENDFORM. ************************************************************************ * Output of the generated table ************************************************************************ FORM DISPLAY-ITAB. * SKIP 2. ULINE. WRITE:/1 'Step 3: The content of table ITAB: ' COLOR 6. ULINE. *....................................................................... MOVE SPACE TO ITAB. LOOP AT ITAB. WRITE: /1 ITAB-CFELD4, ITAB-IFELD, ITAB-PFELD3, ITAB-PFELD5, ITAB-ANZAHL COLOR 3. ENDLOOP. * ENDFORM. ************************************************************************ * Output of head line of ITAB1 ************************************************************************ FORM DISPLAY-KOPFZEILE-ITAB1. * WRITE: 35 ITAB1-CFELD4, ITAB1-IFELD, ITAB1-PFELD3, ITAB1-PFELD5. * ENDFORM. ************************************************************************ ************************************************************************ ************************************************************************ * Example call for the generation * ************************************************************************ *FORM GENERATE-ZUFALL. * * DO 100 TIMES. * PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-IFELD 4 'N'. * PERFORM ERZEUGEN-PSEUDOZUFALL USING ITAB1-CFELD 5 'A'. * APPEND ITAB1. "1 2 3 * ENDDO. *ENDFORM. ********************* DOCU ******************************************** **.....Parameter........................................................ ** 1 = ITAB1-CFELD --> real table field name ** 2 = 5 --> Length in bytes which should be filled ** 3 = A oder N --> A lphabetic content, N umeric Content **.....Limitations...................................................... ** I fields maximum 9 chars ** P fields maximum 30 chars ** C, N-Felder maximum 30 chars * ************************************************************************ ************************************************************************ * Creation of test data * ************************************************************************ FORM ERZEUGEN-PSEUDOZUFALL USING TARGET LAENGE TYP. DATA: E_RAND TYPE F. DATA: Z_TIME(31) TYPE N. DATA: Z_P(16) TYPE P VALUE '999999999999999999996467'. DATA: Z_A(16) TYPE P VALUE '671354420908421773035669'. DATA: Z_C(16) TYPE P. DATA: Z_ZUFALL TYPE F. * GET RUN TIME FIELD Z_TIME. *....................................................................... FIELD-SYMBOLS: <FS1>, <FS2>. DATA: AUSW_NUM(10) TYPE C VALUE '0123456789'. DATA: AUSW_ABC(27) TYPE C VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '. DATA: Z_AUSWAHL(30) TYPE N. DATA: BEGINN TYPE I. DATA: NFELD(30) TYPE N. DATA: CFELD(30) TYPE C. DATA: Z_OFFSET(2) TYPE N. ************************************************************************ IF E_RAND EQ 0. GET RUN TIME FIELD Z_TIME. MOVE SY-UZEIT TO E_RAND. ADD Z_TIME+27(4) TO E_RAND. ADD Z_TIME+26(5) TO E_RAND. CLEAR Z_TIME. ENDIF. *----------------------------------------------------------------------- * DO LAENGE TIMES. E_RAND = ( Z_A * E_RAND + 1 ) MOD Z_P. Z_ZUFALL = E_RAND / Z_P. *....................................................................... IF TYP EQ 'N'. "numeric field DO LAENGE TIMES. Z_ZUFALL = Z_ZUFALL * 10. ENDDO. MOVE Z_ZUFALL TO Z_AUSWAHL. Z_OFFSET = 29 - LAENGE + 1. ASSIGN Z_AUSWAHL+Z_OFFSET(1) TO <FS2>. "can be directly used WRITE <FS2> TO NFELD+BEGINN. ADD 1 TO BEGINN. ENDIF. ************************************************************************ IF TYP EQ 'A'. "Alphabetic field MOVE E_RAND TO Z_AUSWAHL. Z_AUSWAHL = Z_AUSWAHL MOD 27. "26 letters and blank ASSIGN AUSW_ABC+Z_AUSWAHL(1) TO <FS1>. WRITE <FS1> TO CFELD+BEGINN. ADD 1 TO BEGINN. ENDIF. *----------------------------------------------------------------------- E_RAND = E_RAND / 10000000000000. E_RAND = E_RAND / 10000000. ENDDO. * IF TYP EQ 'N'. MOVE NFELD TO TARGET. ENDIF. IF TYP EQ 'A'. MOVE CFELD TO TARGET. ENDIF. * ENDFORM. ************************************************************************ ******************* END OF PROGRAM ************************************* ************************************************************************