Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 153 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
153
Dung lượng
7,76 MB
Nội dung
SAP AG BC - ABAP Programming Appending Table Lines December 1999 309 general rule, where internal tables can be extended dynamically. If you add more lines than specified, the last line is discarded. This is useful for creating ranked lists of limited length (for example "Top Ten"). You can use the APPEND statement to generate ranked lists containing up to 100 entries. When dealing with larger lists, it is advisable to sort [Page 272] tables normally for performance reasons. Examples DATA: BEGIN OF WA, COL1 TYPE C, COL2 TYPE I, END OF WA. DATA ITAB LIKE TABLE OF WA. DO 3 TIMES. APPEND INITIAL LINE TO ITAB. WA-COL1 = SY-INDEX. WA-COL2 = SY-INDEX ** 2. APPEND WA TO ITAB. ENDDO. LOOP AT ITAB INTO WA. WRITE: / WA-COL1, WA-COL2. ENDLOOP. The output is: 0 11 0 24 0 39 This example creates an internal table ITAB with two columns that is filled in the DO loop. Each time the processing passes through the loop, an initialized line is appended and then the table work area is filled with the loop index and the square root of the loop index and appended. DATA: BEGIN OF LINE1, COL1(3) TYPE C, COL2(2) TYPE N, COL3 TYPE I, END OF LINE1, TAB1 LIKE TABLE OF LINE1. DATA: BEGIN OF LINE2, FIELD1(1) TYPE C, FIELD2 LIKE TAB1, END OF LINE2, TAB2 LIKE TABLE OF LINE2. LINE1-COL1 = 'abc'. LINE1-COL2 = '12'. LINE1-COL3 = 3. APPEND LINE1 TO TAB1. BC - ABAP Programming SAP AG Appending Table Lines 310 December 1999 LINE1-COL1 = 'def'. LINE1-COL2 = '34'. LINE1-COL3 = 5. APPEND LINE1 TO TAB1. LINE2-FIELD1 = 'A'. LINE2-FIELD2 = TAB1. APPEND LINE2 TO TAB2. REFRESH TAB1. LINE1-COL1 = 'ghi'. LINE1-COL2 = '56'. LINE1-COL3 = 7. APPEND LINE1 TO TAB1. LINE1-COL1 = 'jkl'. LINE1-COL2 = '78'. LINE1-COL3 = 9. APPEND LINE1 TO TAB1. LINE2-FIELD1 = 'B'. LINE2-FIELD2 = TAB1. APPEND LINE2 TO TAB2. LOOP AT TAB2 INTO LINE2. WRITE: / LINE2-FIELD1. LOOP AT LINE2-FIELD2 INTO LINE1. WRITE: / LINE1-COL1, LINE1-COL2, LINE1-COL3. ENDLOOP. ENDLOOP. The output is: A abc 12 3 def 34 5 B ghi 56 7 jkl 78 9 The example creates two internal tables TAB1 and TAB2. TAB2 has a deep structure because the second component of LINE2 has the data type of internal table TAB1. LINE1 is filled and appended to TAB1. Then, LINE2 is filled and appended to TAB2. After clearing TAB1 with the REFRESH statement, the same procedure is repeated. DATA: BEGIN OF LINE, COL1 TYPE C, COL2 TYPE I, END OF LINE. DATA: ITAB LIKE TABLE OF LINE, JTAB LIKE ITAB. DO 3 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 3. APPEND LINE TO JTAB. ENDDO. APPEND LINES OF JTAB FROM 2 TO 3 TO ITAB. SAP AG BC - ABAP Programming Appending Table Lines December 1999 311 LOOP AT ITAB INTO LINE. WRITE: / LINE-COL1, LINE-COL2. ENDLOOP. The output is: 11 24 39 28 327 This example creates two internal tables of the same type, ITAB and JTAB. In the DO loop, ITAB is filled with a list of square numbers, and JTAB with a list of cube numbers. Then, the last two lines of JTAB are appended to ITAB. DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, COL3 TYPE I, END OF LINE. DATA ITAB LIKE TABLE OF LINE INITIAL SIZE 2. LINE-COL1 = 1. LINE-COL2 = 2. LINE-COL3 = 3. APPEND LINE TO ITAB SORTED BY COL2. LINE-COL1 = 4. LINE-COL2 = 5. LINE-COL3 = 6. APPEND LINE TO ITAB SORTED BY COL2. LINE-COL1 = 7. LINE-COL2 = 8. LINE-COL3 = 9. APPEND LINE TO ITAB SORTED BY COL2. LOOP AT ITAB INTO LINE. WRITE: / LINE-COL2. ENDLOOP. The output is: 8 5 The program inserts three lines into the internal table ITAB using the APPEND statement and the SORTED BY addition. The line with the smallest value for the field COL2 is deleted from the table, since the number of lines that can be appended is fixed through the INITIAL SIZE 2 addition in the DATA statement. BC - ABAP Programming SAP AG Inserting Lines Using the Index 312 December 1999 Inserting Lines Using the Index The INSERT statement allows you not only to insert lines in any type of internal table, but also allows you to change them using a line index. You can insert either a single line or a group of lines into index tables using the index. Inserting a Single Line To insert a line into an index table, use the statement: INSERT <line> INTO <itab> [INDEX <idx>]. <line> is either a work area that is convertible to the line type, or the expression INITIAL LINE. If you use <wa>, the system adds a new line to the internal table <itab> and fills it with the contents of the work area. INITIAL LINE inserts a blank line containing the correct initial value for each field of the structure. If you use the INDEX option, the new line is inserted before the line which has the index <idx>. After the insertion, the new entry has the index <idx> and the index of the following lines is incremented by 1. If the table contains <idx> -1 lines, the new line is added at the end of the table. If the table has less than <idx> - 1 lines, the new line cannot be inserted, and SY-SUBRC is set to 4. When the system successfully adds a line to the table, SY-SUBRC is set to 0. Without the INDEX addition, you can only use the above statement within a LOOP. Then, the new line is inserted before the current line (<idx> is implicitly set to SY-TABIX). Appending lines to standard tables and sorted tables with a non-unique key works regardless of whether lines with the same key already exist in the table. Duplicate entries may occur. A runtime error occurs if you attempt to add a duplicate entry to a sorted table with a unique key. Equally, a runtime error occurs if you violate the sort order of a sorted table by appending to it. Inserting Several Lines To add several lines to an internal table, use the statement: INSERT LINES OF <itab1> INTO <itab2> [INDEX <idx>]. The system inserts the lines of table <itab1> one by one into <itab2> using the same rules as for single lines. ITAB1 can be any type of table. The line type of ITAB1 must be convertible into the line type of ITAB2. When you append an index table to another index table, you can specify the lines to be appended as follows: INSERT LINES OF <itab1> [FROM <n 1 >] [TO <n 2 >] INTO <itab2> [INDEX <idx>]. <n 1 > and <n 2 > specify the indexes of the first and last lines of ITAB1 that you want to insert into ITAB2. Depending on the size of the tables and where they are inserted, this method of inserting lines of one table into another can be up to 20 times faster than inserting them line by line in a loop. SAP AG BC - ABAP Programming Inserting Lines Using the Index December 1999 313 Examples DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE TABLE OF LINE. DO 2 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. ENDDO. LINE-COL1 = 11. LINE-COL2 = 22. INSERT LINE INTO ITAB INDEX 2. INSERT INITIAL LINE INTO ITAB INDEX 1. LOOP AT ITAB INTO LINE. WRITE: / SY-TABIX, LINE-COL1, LINE-COL2. ENDLOOP. The output is: 100 211 31122 424 The example creates an internal table ITAB and fills it with two lines. A new line containing values is inserted before the second line. Then, an initialized line is inserted before the first line. DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE TABLE OF LINE. DO 2 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. ENDDO. LOOP AT ITAB INTO LINE. LINE-COL1=3*SY-TABIX. LINE-COL2=5*SY-TABIX. INSERT LINE INTO ITAB. ENDLOOP. BC - ABAP Programming SAP AG Inserting Lines Using the Index 314 December 1999 LOOP AT ITAB INTO LINE. WRITE: / SY-TABIX, LINE-COL1, LINE-COL2. ENDLOOP. The output is: 13 5 21 1 3915 42 4 The example creates an internal table ITAB and fills it with two lines. Using a LOOP construction, the program inserts a new line before each existing line. DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA: ITAB LIKE TABLE OF LINE, JTAB LIKE ITAB. DO 3 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 3. APPEND LINE TO JTAB. ENDDO. INSERT LINES OF ITAB INTO JTAB INDEX 1. LOOP AT JTAB INTO LINE. WRITE: / SY-TABIX, LINE-COL1, LINE-COL2. ENDLOOP. The output is: : 11 1 22 4 33 9 41 1 52 8 6327 The example creates two internal tables of the same type. Each is filled with three lines. Then, the entire table ITAB is inserted before the first line of JTAB. SAP AG BC - ABAP Programming Reading Lines Using the Index December 1999 315 Reading Lines Using the Index You can use the READ statement to read lines in tables using their index. To read a single line of an index table, use the statement: READ TABLE <itab> INDEX <idx> <result>. The system reads the line with the index <idx> from the table <itab>. This is quicker than searching using the key [Page 288]. The <result> part can specify a further processing option for the line that is retrieved. If an entry with the specified index was found, the system field SY-SUBRC is set to 0 and SY- TABIX contains the index of that line. Otherwise, SY-SUBRC is set to a value other than 0. If <idx> is less than or equal to 0, a runtime error occurs. If <idx> is greater than the number of lines in the table, SY-SUBRC is set to 4. Example DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1. FIELD-SYMBOLS <FS> LIKE LINE OF ITAB. DO 20 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2=2*SY-INDEX. APPEND LINE TO ITAB. ENDDO. READ TABLE ITAB ASSIGNING <FS> INDEX 7. WRITE: SY-SUBRC, SY-TABIX. WRITE: / <FS>-COL1, <FS>-COL2. The output is: 07 714 The example creates a sorted table ITAB and fills it with 20 lines. The line with index 7 is read and assigned to the field symbol <FS>. BC - ABAP Programming SAP AG Binary Search in Standard Tables 316 December 1999 Binary Search in Standard Tables If you read entries [Page 288] from standard tables using a key other than the default key, you can use a binary search instead of the normal linear search. To do this, include the addition BINARY SEARCH in the corresponding READ statements. READ TABLE <itab> WITH KEY = <f> <result> BINARY SEARCH. and READ TABLE <itab> WITH KEY <k1> = <f1> <kn> = <fn> <result> BINARY SEARCH. The standard table must be sorted in ascending order by the specified search key. The BINARY SEARCH addition means that you can access an entry in a standard table by its key as quickly as you would be able to in a sorted table. Example DATA: BEGIN OF LINE, COL1 TYPE I, COL2 TYPE I, END OF LINE. DATA ITAB LIKE STANDARD TABLE OF LINE. DO 4 TIMES. LINE-COL1 = SY-INDEX. LINE-COL2 = SY-INDEX ** 2. APPEND LINE TO ITAB. ENDDO. SORT ITAB BY COL2. READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH. WRITE: 'SY-SUBRC =', SY-SUBRC. The output is: SY-SUBRC = 0 The program fills a standard table with a list of square numbers and sorts them into ascending order by field COL2. The READ statement uses a binary search to look for and find the line in the table where COL2 has the value 16. SAP AG BC - ABAP Programming Finding Character Strings in Internal Tables December 1999 317 Finding Character Strings in Internal Tables To find a string in a line of an index table, use the following statement: SEARCH <itab> FOR <str> <options>. The statement searches the internal table <itab> for the character string <str>. If the search is successful, SY-SUBRC is set to 0, and SY-TABIX is set to the index of the table line in which the string was found. SY-FDPOS contains the offset position of the string in the table line. Otherwise, SY-SUBRC is set to 4. The statement treats all table lines as type C fields, regardless of their actual line type. There is no conversion. The search string <str> can have the same form as for a normal string search [Page 171] in a field. The different options (<options>) for the search in an internal table <itab> are: • ABBREVIATED Field <c> is searched for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word and the string <str> must be the same. • STARTING AT <lin 1 > Searches table <itab> for <str>, starting at line <lin 1 >.<lin 1 > can be a variable. • ENDING AT <lin 2 > Searches table <itab> for <str> up to line <lin 2 >.<lin 2 > can be a variable. • AND MARK If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case. This statement only works with index tables. There is no corresponding statement for hashed tables. Example DATA: BEGIN OF LINE, INDEX TYPE I, TEXT(8) TYPE C, END OF LINE. DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY INDEX. DATA NUM(2) TYPE N. DO 10 TIMES. LINE-INDEX = SY-INDEX. NUM = SY-INDEX. CONCATENATE 'string' NUM INTO LINE-TEXT. APPEND LINE TO ITAB. ENDDO. BC - ABAP Programming SAP AG Finding Character Strings in Internal Tables 318 December 1999 SEARCH ITAB FOR 'string05' AND MARK. WRITE: / '''string05'' found at line', (1) SY-TABIX, 'with offset', (1) SY-FDPOS. SKIP. READ TABLE ITAB INTO LINE INDEX SY-TABIX. WRITE: / LINE-INDEX, LINE-TEXT. The output is: 'string05' found at line 5 with offset 4 5 STRING05 The offset of the string found in the table is determined by the width of the first table column, which has type I and length 4. The option AND MARK changes the table contents in the corresponding line. [...]... ENDDO LINE-COL2 = 222 MODIFY ITAB FROM LINE INDEX 2 TRANSPORTING (NAME) LINE-COL1 = 3 LINE-COL2 = 33 3 MODIFY ITAB FROM LINE INDEX 3 LOOP AT ITAB INTO LINE WRITE: / SY-TABIX, LINE-COL1, LINE-COL2 ENDLOOP The output is: 32 0 December 1999 SAP AG BC - ABAP Programming Changing Table Lines Using the Index 1 2 3 4 1 2 3 4 1 222 33 3 16 The example fills a sorted table with four lines In the second and third lines,... each field group Defining an Extract [Page 33 3] 2 Fill the extract line by line by extracting the required data Filling an Extract with Data [Page 33 5] 3 Once you have filled the extract, you can sort it and process it in a loop At this stage, you can no longer change the contents of the extract Processing Extracts [Page 33 7] 33 2 December 1999 SAP AG BC - ABAP Programming Defining an Extract Defining... as follows: 1 1 2 100 3 9 The statements in the program that does not use a header line are easier to understand As a further measure, you could have a further work area just to specify the key of the internal table, but to which no other values from the table are assigned 33 0 December 1999 SAP AG BC - ABAP Programming Using Header Lines as Work Areas December 1999 33 1 BC - ABAP Programming SAP AG Extracts... SY-INDEX December 1999 32 3 BC - ABAP Programming SAP AG Deleting Lines Using the Index LINE-COL2 = SY-INDEX ** 2 APPEND LINE TO ITAB ENDDO DELETE ITAB FROM 3 TO 38 WHERE COL2 > 20 LOOP AT ITAB INTO LINE WRITE: / LINE-COL1, LINE-COL2 ENDLOOP The output is: 1 1 2 4 3 9 4 16 39 1.521 40 1.600 The program deletes all entries from the standard table ITAB with an index between 3 and 39 where the value in... SPFLI-CITYFROM, December 1999 33 5 BC - ABAP Programming SAP AG Filling an Extract with Data and SPFLI-CITYTO The first three fields belong to the prefixed field group HEADER The records of the field group FLIGHT_DATE consist only of the three fields of field group HEADER The following figure shows the structure of the extract dataset: 33 6 December 1999 SAP AG BC - ABAP Programming Processing Extracts... required data, you can process it When you have started processing it, you can no longer extract data into it Reading the Extract [Page 33 8] Sorting the Extract [Page 34 1] Control Level Processing [Page 34 4] Calculating Numbers and Totals [Page 34 8] December 1999 33 7 BC - ABAP Programming SAP AG Reading an Extract Reading an Extract Like internal tables, you can read the data in an extract dataset using a... ITAB ENDDO DELETE ITAB INDEX: 2, 3, 4 WRITE: 'SY-SUBRC =’, SY-SUBRC SKIP LOOP AT ITAB INTO LINE WRITE: / SY-TABIX, LINE-COL1, LINE-COL2 ENDLOOP 32 2 December 1999 SAP AG BC - ABAP Programming Deleting Lines Using the Index The output is: SY-SUBRC 4 1 2 3 1 3 5 1 9 25 The example fills a sorted table ITAB with five lines Then it deletes the three lines with the indexes 2, 3, and 4 After deleting the line... ITAB with 30 lines In the loop, only the lines 10 to 25 are read There is also a condition that the contents of COL2 must be more than 400 December 1999 32 5 BC - ABAP Programming SAP AG Specifying the Index in Loops 32 6 December 1999 SAP AG BC - ABAP Programming Access Using Field Symbols Access Using Field Symbols When you read table entries using READ or in a LOOP, you can assign them to a field symbol... empty December 1999 33 3 BC - ABAP Programming SAP AG Defining an Extract NODES: SPFLI, SFLIGHT FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE INTO HEADER, SPFLI-CITYFROM SPFLI-CITYTO INTO FLIGHT_INFO The program is linked to the logical database [Page 1210] F1S The NODES statement declares the corresponding interface work areas [Page 131 ] There are three... December 1999 32 7 BC - ABAP Programming SAP AG Access Using Field Symbols DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1 FIELD-SYMBOLS LIKE LINE OF ITAB DO 4 TIMES LINE-COL1 = SY-INDEX LINE-COL2 = SY-INDEX ** 2 APPEND LINE TO ITAB ENDDO READ TABLE ITAB WITH TABLE KEY COL1 = 2 ASSIGNING -COL2 = 100 READ TABLE ITAB WITH TABLE KEY COL1 = 3 ASSIGNING DELETE ITAB INDEX 3 IF . (NAME). LINE-COL1 = 3. LINE-COL2 = 33 3. MODIFY ITAB FROM LINE INDEX 3. LOOP AT ITAB INTO LINE. WRITE: / SY-TABIX, LINE-COL1, LINE-COL2. ENDLOOP. The output is: SAP AG BC - ABAP Programming Changing. must be more than 400. BC - ABAP Programming SAP AG Specifying the Index in Loops 32 6 December 1999 SAP AG BC - ABAP Programming Access Using Field Symbols December 1999 32 7 Access Using Field. is: 11 24 39 416 39 1.521 40 1.600 The program deletes all entries from the standard table ITAB with an index between 3 and 39 where the value in COL2 is greater than 20. SAP AG BC - ABAP Programming Specifying