📄 ch12.htm
字号:
and places it in the body of <TT>it2</TT>. Any existing contents
in <TT>it2</TT> are overwritten. The contents of the header lines,
if either internal table has one, remain unchanged. This is the
most efficient way to copy the contents from one internal table
to another.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Two internal tables have the same structure if 1) they both have the same number of components, and 2) the data type and length of each component is the same as the corresponding component of the other internal table. Only the component names do not have to match.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="CopyingaPortionofanInternalTable">
Copying a Portion of an Internal Table</A></H3>
<P>
If you want to copy a portion of an internal table to another,
or if you want to leave the contents of the target table in place,
use the <TT>append lines</TT> and <TT>insert lines</TT> statements.
<H4>Using the append lines Statement</H4>
<P>
Use the <TT>append lines</TT> statement when you want to append
rows to the end of the target table.
<H5>Syntax for the append lines Statement</H5>
<BLOCKQUOTE>
The following is the syntax for the <TT>append lines</TT> statement.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
append lines of <I>it1</I> [from <I>nf</I>] [to <I>nt</I>] to <I>it2</I>.
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
where:
<UL>
<LI><TT><I>it1</I></TT> and <TT><I>it2</I></TT>
are internal tables with or without header lines.
<LI><TT><I>nf</I></TT> and <TT><I>nt</I></TT>
are numeric variables, literals, or constants.
</UL>
The following points apply:
<UL>
<LI>The structures of <TT><I>it1</I></TT>
and <TT><I>it2</I></TT> must match.
<LI><TT>nf</TT> is the index of the first row to be copied from
<TT>it1</TT>. If the <TT>from</TT> addition is not specified,
copying begins from the first row of <TT>it1</TT>.
<LI><TT>nt</TT> is the index of the last row to be copied from
<TT>it1</TT>. If the <TT>to</TT> addition is not specified, copying
continues to the last row of <TT>it1</TT>.
<LI>If neither <TT>from</TT> nor <TT>to</TT> are specified, the
entire table is appended.
<LI>After the <TT>append lines</TT> statement has executed, <TT>sy-tabix</TT>
contains the number of rows in the table.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Using <TT>append</TT> lines is three to four times faster than using <TT>append</TT> to add the rows one at a time.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
</BLOCKQUOTE>
<P>
<H4>Using the insert lines Statement</H4>
<P>
Use the <TT>insert lines</TT> statement when you want to insert
rows at a place other than the end into the target table.
<H5>Syntax for the insert lines Statement</H5>
<BLOCKQUOTE>
The following is the syntax for the <TT>insert lines</TT> statement.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
insert lines of <I>it1</I> [from <I>nf</I>] [to <I>nt</I>] into <I>it2</I> [index <I>nb</I>].
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
where:
<UL>
<LI><TT><I>it1</I></TT> and <TT><I>it2</I></TT>
are internal tables with or without header lines.
<LI><TT><I>nf</I></TT>, <TT><I>nt</I></TT>,
and <TT><I>nb</I></TT> are numeric
variables, literals, or constants.
</UL>
All of the points that apply to the <TT>append lines</TT> statement
also apply here. The difference is that rows from <TT><I>it1</I></TT>
are inserted into <TT><I>it2</I></TT>
before row number <TT><I>nb</I></TT>.
If the value of <TT><I>nb</I></TT>
is the number of rows in <TT><I>it2</I></TT>
plus 1, the row is appended to the end of <TT><I>it2</I></TT>.
If <TT><I>nb</I></TT> is greater
than that, the row is not appended and <TT>sy-subrc</TT> is set
to <TT>4</TT>. If <TT><I>nb</I></TT>
is less than 1, a runtime error occurs.
</BLOCKQUOTE>
<BLOCKQUOTE>
You can use this statement inside or outside of <TT>loop at <I>it2</I></TT>.
If used outside, you must specify the <TT>index</TT> addition.
Inside, <TT>index</TT> is optional. If it is not specified, the
current row number in <TT><I>it2</I></TT>
is assumed.
</BLOCKQUOTE>
<H3><A NAME="SampleProgramthatCopiesDataBetweenInternalTables">
Sample Program that Copies Data Between Internal Tables</A></H3>
<P>
Listing 12.2 shows a sample program that copies data from one
internal table to another.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.2 This Program Copies the Data from One
Internal Table to Another Using the append lines and insert lines
Statements<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1202.
2 data: begin of it1 occurs 10,
3 f1,
4 end of it1,
5 it2 like it1 occurs 10 with header line,
6 alpha(10) value 'ABCDEFGHIJ'.
7
8 do 10 times varying it1-f1 from alpha+0 next alpha+1.
9 append it1.
10 enddo.
11
12 append lines of it1 from 2 to 5 to it2.
13 loop at it2.
14 write it2-f1.
15 endloop.
16
17 insert lines of it1 from 8 into it2 index 2.
18 skip.
19 loop at it2.
20 write it2-f1.
21 endloop.
22
23 loop at it2.
24 if it2-f1 >= 'E'.
25 insert lines of it1 to 1 into it2.
26 endif.
27 endloop.
28
29 skip.
30 loop at it2.
31 write it2-f1.
32 endloop.
33
34 skip.
35 it2[] = it1[].
36 loop at it2.
37 write it2-f1.
38 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.2 produces this output:
<BLOCKQUOTE>
<PRE>
B C D E
B H I J C D E
B A H A I A J C D A E
A B C D E F G H I J
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 8 through 10 fill <TT>it1</TT> with 10 rows containing
the first 10 letters of the alphabet.
<LI>Line 12 appends rows 2 through 5 of <TT>it1</TT> to <TT>it2</TT>.
<TT>it2</TT> now has four rows containing the letters <TT>B</TT>
through <TT>E</TT>.
<LI>On line 17, the <TT>to</TT> addition is not specified, so
the end of <TT>it1</TT> is assumed. This inserts rows 8, 9, and
10 from <TT>it1</TT> into <TT>it2</TT> before row 2.
<LI>On line 24, if the letter in <TT>it2-f1</TT> is greater than
or equal to <TT>E</TT>, row 1 of <TT>it1</TT> is inserted before
the current row of <TT>it2</TT>. (The <TT>from</TT> addition is
not specified, so the beginning of <TT>it1</TT> is assumed.) This
results in four rows being inserted. In the output, they are the
<TT>'A'</TT> values.
<LI>Line 35 copies the contents of <TT>it1</TT> to <TT>it2</TT>,
completely overlaying the existing contents.
</UL>
<H3><A NAME="ComparingtheContentsofTwoInternalTables">
Comparing the Contents of Two Internal Tables</A></H3>
<P>
You can use the table body operator to compare the contents of
two internal tables, as shown here:
<BLOCKQUOTE>
<PRE>
if it1[] = it2[].
</PRE>
</BLOCKQUOTE>
<P>
To use this construct, the internal tables must have the same
structure. If they do not, you will have to compare them manually,
row by row.
<P>
This statement is true when <TT>it1</TT> and <TT>it2</TT> contain
the same number of rows and the contents of each row are the same.
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The If - Equal to statement is the most efficient way to compare the con-tents of two internal tables.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="UsingtheeditorcallStatement"><FONT SIZE=5 COLOR=#FF0000>
Using the editor-call Statement</FONT></A></H2>
<P>
The <TT>editor-call</TT> statement displays the contents of an
internal table to the user in an editor similar to the ABAP/4
source code editor. It is useful for debugging and as a simple
interface for allowing the user to enter and modify data in tabular
form.
<H3><A NAME="SyntaxfortheeditorcallStatement">
Syntax for the editor-call Statement</A></H3>
<P>
The following is the syntax for the <TT>editor-call</TT> statement.
<BLOCKQUOTE>
<PRE>
editor-call for <I>it</I> [title <I>t</I>] [display mode]
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is the
name of an internal table.
<LI><TT><I>t</I></TT> is a literal,
constant, or variable.
</UL>
<P>
The following points apply:
<UL>
<LI><TT><I>it</I></TT> can only
contain type <TT>c</TT> components.
<LI>The maximum length for a row is 72 characters.
<LI><TT>t</TT> is the text displayed in the title bar of the editor
window.
</UL>
<P>
The <TT>display mode</TT> addition causes the data to be displayed
in the editor in display mode. The user will be able to search
and scroll, but will not be able to change the contents.
<P>
After viewing or modifying the internal table contents via the
editor, the user presses one of these buttons: Save, Back, Exit,
or Cancel. Save saves the changes made to the internal table contents
and returns to the program. Back, Exit, and Cancel leave the editor
and return to the program. If changes have been made, the user
is prompted to save or cancel the changes.
<P>
After the <TT>editor-call</TT> statement has executed, <TT>sy-subrc</TT>
is set to the values shown in Table 12.2.<BR>
<P>
<CENTER><B>Table 12.2 Values of </B><TT><B>SY-SUBRC</B></TT><B>
After the </B><TT><B>EDITOR-CALL</B></TT><B>
Statement</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>sy-subrc</B></CENTER></TD><TD WIDTH=384><CENTER><B>Meaning</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>0</TT></CENTER></TD><TD WIDTH=384>A save was performed. The contents of the internal table might or might not be changed.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>4</TT></CENTER></TD><TD WIDTH=384>The user did not perform a save. The contents of the internal table are unchanged.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 12.3 shows a sample program that uses the <TT>editor-call</TT>
statement. In this example, the internal table is filled with
five lines and displayed in the editor so that the user can modify
the data. The contents are then written out, and a message is
also written to indicate whether a change was performed.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.3 Use the editor-call Statement to View,
Edit, and Debug the Contents of an Internal Table.<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1203.
2 data: begin of it occurs 10,
3 t(72), "text
4 end of it,
5 save_it like it occurs 10. "will contain copy of the original
6
7 it-t = 'Name :'. append it.
8 it-t = 'Address :'. append it.
9 it-t = 'Phone :'. append it.
10 it-t = 'Freeform Text '. append it.
11 clear it-t with '-'. append it.
12
13 save_it = it[]. "same as: save_it[] = it[].
14 editor-call for it title 'Freeform Entry'.
15 if sy-subrc = 4. "user did not perform a save
16 write: / 'Data was not changed'.
17 elseif save_it[] <> it[]. "user performed a save
18 write: / 'Data was changed'.
19 else.
20 write: / 'Data was not changed'.
21 endif.
22 write: / sy-uline(72).
23 loop at it.
24 write: / it-t.
25 endloop.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
If no data is entered when the editor is displayed, the code in
Listing 12.3 produces this output:
<BLOCKQUOTE>
<PRE>
Data was not changed
-----------------------------------------------------------------------
Name :
Address :
Phone :
Freeform Text
-------------------------------------------------------------------
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 4 define an internal table having a single
component <TT>t</TT>, character length <TT>72</TT>.
<LI>Line 5 defines a second internal table like the first. It
will be used to hold a reference copy of the data in <TT>it</TT>.
It does not have a header line. The header line has been left
off because it is not needed in this program.
<LI>Lines 7 through 11 append five lines to the internal table
from the header line.
<LI>Line 13 copies the body of <TT>it</TT> to the body of <TT>save_it</TT>.
Because <TT>save_it</TT> does not have a header line, the left
side of the assignment can be written with or without square brackets.
<LI>Line 14 displays the contents of the internal table in the
editor with the title <TT>Freeform Entry</TT>.
<LI>Line 15 checks the value of <TT>sy-subrc</TT> to determine
whether the user performed a save. If he did not, it is not possible
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -