⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
that data was changed and a message is written out.
<LI>Line 17 compares the new contents of <TT>it</TT> with the
reference copy in <TT>save_it</TT>. If they are different, a message
is written out. If they are the same, line 20 writes out a message
to indicate this.
<LI>Lines 22 writes out an underline 72 characters long.
<LI>Lines 23 through 25 write out the new contents of the internal
table, including any user modifications.
</UL>
<H2><A NAME="InsertingRowsintoanInternalTable"><FONT SIZE=5 COLOR=#FF0000>
Inserting Rows into an Internal Table</FONT></A></H2>
<P>
To insert a single row into an internal table, use the <TT>insert</TT>
statement.
<H3><A NAME="SyntaxfortheinsertStatement">
Syntax for the insert Statement</A></H3>
<P>
The following is the syntax for the <TT>insert</TT> statement.
<H3><A NAME="insertwaintoitindexn">
insert [wa into] it [index n]</A></H3>
<P>
where:
<UL>
<LI><TT><I>wa</I></TT> is a work
area with the same structure as a row of internal table <TT><I>it</I></TT>.
<LI><TT><I>n</I></TT> is a numeric
literal, variable, or constant.
</UL>
<P>
The following points apply:
<UL>
<LI>If <TT><I>wa</I></TT> is specified,
the contents of <TT><I>wa</I></TT>
are inserted into <TT><I>it</I></TT>.
<TT><I>wa</I></TT> must have the
same structure as <TT><I>it</I></TT>.
<LI>If <TT>wa</TT> is not specified, the contents of the header
line are inserted into <TT>it</TT>. If <TT>it</TT> does not have
a header line, <TT>wa</TT> must be specified.
<LI>If <TT>index</TT> is specified, the new row is inserted before
row <TT>n</TT>. Row <TT>n</TT> then becomes row <TT>n</TT>+1.
<LI>The <TT>insert</TT> statement can be used inside or outside
of <TT>loop at it</TT>. If it is outside, the <TT>index</TT> addition
must be specified. If it is inside, <TT>index</TT> is optional.
If it is not specified, the current row is assumed.
</UL>
<P>
Listing 12.4 contains a sample program that uses the <TT>insert</TT>
statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.4&nbsp;&nbsp;Use the insert Statement to Insert
a Single Row into an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1  report ztx1204.
 2  data: begin of it occurs 5,
 3            f1 like sy-index,
 4            end of it.
 5 
 6  do 5 times.
 7      it-f1 = sy-index.
 8      append it.
 9      enddo.
10
11 it-f1 = -99.
12 insert it index 3.
13
14 loop at it.
15     write / it-f1.
16     endloop.
17
18 loop at it where f1 &gt;= 4.
19     it-f1 = -88.
20     insert it.
21     endloop.
22
23 skip.
24 loop at it.
25     write / it-f1.
26     endloop.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 12.4 produces this output:
<BLOCKQUOTE>
<PRE>
         1     
         2     
        99-    
         3     
         4     
         5     
               
         1     
         2     
        99-    
         3     
        88-    
         4     
        88-    
         5     
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 6 through 9 append five rows containing the numbers
1 through 5 to <TT>it</TT>.
<LI>Line 11 assigns to the header line component <TT>it-f1</TT>
a value of <TT>-99</TT>.
<LI>Line 12 inserts the header line of <TT>it</TT> as new row
into the body of <TT>it</TT> before row 3. The existing row 3
becomes row 4 after the insert.
<LI>Line 18 retrieves those rows from the internal table that
have an <TT>f1</TT> value greater than or equal to 4. Before each
row, line 20 inserts a new row from the header line of <TT>it</TT>.
Prior to the insert, line 19 changed the <TT>f1</TT> component
to contain <TT>-88</TT>.
</UL>
<P>
<B><FONT SIZE=5 COLOR=#FFFFFF FACE="Frutiger-Black">12</FONT></B><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
After each <TT>insert </TT>statement is executed, the system re-indexes all rows below the one inserted. This introduces overhead when you insert rows near the top of a large internal table. If you need to insert a block of rows into a large internal table, prepare another table with the rows to be inserted and use <TT>insert lines </TT>instead. The rows in the target table will only be re-indexed once, after this statement has executed.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
When inserting a new row into <TT>it</TT> inside of a <TT>loop
at it</TT>, the insert does not affect the internal table immediately,
but instead it becomes effective on the next loop pass. When inserting
a row <I>after</I> the current row, the table is re-indexed at
the <TT>endloop</TT>, <TT>sy-tabix</TT> is incremented, and the
next loop pass processes the row pointed to by <TT>sy-tabix</TT>.
For example, suppose you are in the third loop pass and you insert
a record before row 4. When <TT>endloop</TT> is executed, the
new row becomes row 4, the old row 4 becomes row 5, and so on.
<TT>Sy-tabix</TT> is incremented by 1, and the next loop pass
processes the newly inserted record.
<P>
If, inside of a loop, you insert a row <I>before</I> the current
row, the table is again re-indexed at the <TT>endloop</TT>. This
time, however, <TT>sy-tabix</TT> is incremented by 1 plus the
number of rows inserted before the current row. The next time
through the loop, the row following the current row is processed.
Suppose, for example, in the third loop pass you insert a row
before row 3. At the <TT>endloop</TT>, the new row becomes row
3, row 3 becomes row 4, and so on. The row you just processed
now has an index of 4. <TT>sy-tabix</TT> is incremented by 2,
which gives 5. Row 4 was re-indexed to 5, so it is processed on
the next loop pass.
<H2><A NAME="ModifyingRowsinanInternalTable"><FONT SIZE=5 COLOR=#FF0000>
Modifying Rows in an Internal Table</FONT></A></H2>
<P>
To modify the contents of one or more rows of an internal table,
use the <TT>modify</TT> statement.
<H3><A NAME="SyntaxforthemodifyStatement">
Syntax for the modify Statement</A></H3>
<P>
The following is the syntax for the <TT>modify</TT> statement.
<BLOCKQUOTE>
<PRE>
modify <I>it</I> [from <I>wa</I>] [index <I>n</I>] [transporting <I>c1</I> <I>c2</I> ... [where <I>exp</I>]]
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is the
name of an internal table with or without a header line.
<LI><TT><I>wa</I></TT> is a work
area with the same structure as a row in the body of <TT><I>it</I></TT><I>.</I>
<LI><TT><I>n</I></TT> is a numeric
literal, variable, or constant.
<LI><TT><I>c1</I></TT> and <TT><I>c2</I></TT>
are components of <TT><I>it</I></TT><I>.</I>
<LI><TT><I>exp</I></TT> is a logical
expression involving components of <TT><I>it</I></TT><I>.</I>
</UL>
<P>
The following points apply:
<UL>
<LI>If <TT>from <I>wa</I></TT>
is specified, the row is overwritten with the contents of <TT><I>wa</I></TT>.
<LI>If <TT>from wa</TT> is not specified, the row is overwritten
with the contents of the header line.
<LI>If <TT>index n</TT> is specified, <TT>n</TT> identifies the
number of the row that is overwritten.
<LI><TT>modify it</TT> can be specified inside or outside of <TT>loop
at it</TT>. If it is outside, <TT>index <I>n</I></TT>
must be specified. When inside, <TT>index <I>n</I></TT>
is optional. If it is not specified, the current row is modified.
</UL>
<P>
<TT>transporting</TT> specifies which components are to be overwritten.
Without it, all are overwritten. With it, only the specified components
are overwritten. The rest remain unchanged.
<P>
Specifying a <TT>where</TT> condition after <TT>transporting</TT>
causes the specified components to be overwritten in all rows
that satisfy the <TT>where</TT> clause. The left-hand side of
each part of <TT><I>exp</I></TT>
must specify a component of <TT><I>it</I></TT>.
The same component can be specified both after <TT>transporting</TT>
and in <TT><I>exp</I></TT>.
<P>
You can't use <TT>modify it</TT> with <TT>where</TT>:
<UL>
<LI>Inside of <TT>loop at it</TT>
<LI>With the <TT>index</TT> addition
</UL>
<P>
Listing 12.5 shows a sample program that modifies the contents
of an internal table.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.5&nbsp;&nbsp;Use modify to Overwrite the Existing
Contents of One or More Rows of an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1  report ztx1205.
 2  data: begin of it occurs 5,
 3            f1 like sy-index,
 4            f2,
 5            end of it,
 6        alpha(5) value 'ABCDE'.
 7 
 8  do 5 times varying it-f2 from alpha+0 next alpha+1.
 9      it-f1 = sy-index.
10     append it.
11     enddo.
12
13 it-f2 = 'Z'.
14 modify it index 4.
15
16 loop at it.
17     write: / it-f1, it-f2.
18     endloop.
19
20 loop at it.
21     it-f1 = it-f1 * 2.
22     modify it.
23     endloop.
24
25 skip.
26 loop at it.
27     write: / it-f1, it-f2.
28     endloop.
29
30 it-f2 = 'X'.
31 modify it transporting f2 where f1 &lt;&gt; 10.
32
33 skip.
34 loop at it.
35     write: / it-f1, it-f2.
36     endloop.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 12.5 produces this output:
<BLOCKQUOTE>
<PRE>
         1  A  
         2  B  
         3  C  
         5  Z  
         5  E  
               
         2  A  
         4  B  
         6  C  
        10  Z  
        10  E  
               
         2  X  
         4  X  
         6  X  
        10  Z  
        10  E  
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 8 through 11 add five rows to <TT>it</TT>. Each row
contains a sequential number and letter of the alphabet.
<LI>Line 13 modifies the contents of <TT>it-f2</TT>, giving it
a value of <TT>'Z'</TT>. <TT>it-f1</TT> is not modified, so it
still contains the last value added to the table: <TT>5</TT>.
<LI>Line 14 overwrites row 4 with the contents of the header line,
changing <TT>f1</TT> to <TT>5</TT> and <TT>f2</TT> to <TT>Z</TT>.
<LI>Lines 20 through 23 loop through all rows of <TT>it</TT>,
placing each row one at a time into the header line. Line 21 multiplies
by 2 the contents of header line component <TT>f1</TT>. Line 22
copies the contents of the header line back into the current row
in the body of <TT>it</TT>, overwriting it.
<LI>Line 30 modifies the contents of the header line component
<TT>f2</TT>, assigning it a value of '<TT>X</TT>'.
<LI>Line 31 modifies all rows in the body where <TT>f1</TT> is
not equal to 10. Only the value of <TT>f2</TT> is copied from
the header line and overwrites the <TT>f2</TT> values in the body.
<TT>f1</TT> remains unchanged in the body and in the header line.
</UL>
<H2><A NAME="DeletingInternalTableContents"><FONT SIZE=5 COLOR=#FF0000>
Deleting Internal Table Contents</FONT></A></H2>
<P>
To delete contents of an internal table, you can use the following
statements:
<UL>
<LI><TT>free</TT>
<LI><TT>refresh</TT>
<LI><TT>clear</TT>
<LI><TT>delete</TT>
</UL>
<H3><A NAME="UsingfreetoDeleteInternalTableContents">
Using free to Delete Internal Table Contents</A></H3>
<P>
Use the <TT>free</TT> statement to delete all rows from an internal
table and free the associated memory.
<H4>Syntax for the free Statement</H4>
<P>
The following is the syntax for the <TT>free</TT> statement.
<BLOCKQUOTE>
<PRE>
free it.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is an internal
table with or without a header line.
</UL>
<P>
The following points apply:
<UL>
<LI>All rows are deleted and all memory used by the body of the
internal table is freed.
<LI>The header line, if it exists, remains unchanged.
</UL>
<P>
Use <TT>free</TT> when you are finished using an internal table.
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Although the memory for internal tables is automatically freed when your program ends, freeing it yourself is usually more efficient. The reason for this lies in the fact that when the output is displayed to the user, technically your program has not yet ended. All resources remain allocated and the pro-gram does not end until the user presses the Back button. This finally ends your program and frees all internal table contents. You can free the internal tables sooner by putting <TT>free </TT>statements at the end of your program. The internal table contents will be released before the user sees the list instead of after.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 12.6 shows how to use the <TT>free</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -