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

📄 ch12.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
41
42 read table it with key f1 = 'K' binary search.
43 write: /, / 'sy-subrc=', sy-subrc, 'sy-tabix=', sy-tabix, / ''.
44 if sy-subrc = 0.
45     delete it index sy-tabix.
46     endif.
47
48 skip.
49 loop at it.
50     write: / sy-tabix, it-f1.
51     endloop.
52
53 free it.
</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.9 produces this output:
<BLOCKQUOTE>
<PRE>
         1  A
         2  B
         3  C
         4  D
         5  E
         6  F
         7  G
         8  H
         9  I
        10  J
        11  K
        12  L

         1  A
         2  B
         3  C
         4  D
         5  F
         6  G
         7  H
         8  I
         9  J
        10  K
        11  L

         1  A
         2  B
         3  C
         4  D
         5  F
         6  J
         7  K
         8  L

         1  A
         2  F
         3  J
         4  K
         5  L

         1  A
         2  K
         3  L

sy-subrc=     0  sy-tabix=          2

         1  A
         2  L
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 7 through 9 fill <TT>it</TT> with 12 rows containing
the values <TT>'A'</TT> through <TT>'L'</TT>.
<LI>Lines 11 through 13 write out the contents of the internal
table.
<LI>Line 15 deletes the fifth row, removing <TT>'E'</TT> from
the table. The sixth row becomes the fifth, the seventh becomes
the sixth, and so on.
<LI>Line 21 deletes the sixth through eighth rows, removing <TT>G</TT>,
<TT>H</TT>, and <TT>I</TT> from the table. The ninth row becomes
the sixth, and so on.
<LI>Line 27 deletes rows that have <TT>f1</TT> values between
<TT>'B'</TT> and <TT>'D'</TT>, inclusive. This causes the second,
third and fourth rows to be deleted.
<LI>Line 33 retrieves rows having an <TT>f1</TT> value between
<TT>'E'</TT> and <TT>'J'</TT>, inclusive. Rows 2 and 3 meet the
criteria. The <TT>delete</TT> statement on line 34 doesn't have
any additions, so it deletes the current row on each pass of the
loop. This causes the second and third rows to be deleted.
<LI>Line 42 locates the row having an <TT>f1</TT> value of <TT>'K'</TT>.
Although <TT>it</TT> only contains three rows, <TT>binary search</TT>
is included for the sake of good example. Row 2 matches, so <TT>sy-subrc</TT>
is set to zero and <TT>sy-tabix</TT> is set to <TT>2</TT>.
<LI>Line 45 is executed because <TT>sy-subrc</TT> is zero. It
deletes row 2, removing <TT>'K'</TT> from the internal table.
</UL>
<P>
Like inserts, deletes inside of a <TT>loop at it</TT> do not affect
the internal table immediately, but instead become effective on
the next loop pass. When deleting 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 delete row 4. When <TT>endloop</TT> is executed,
the row is deleted, row 5 becomes row 4, and so on. <TT>sy-tabix</TT>
is incremented by 1 and the next loop pass processes the next
record.
<P>
If, when inside a loop, you delete 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 minus the
number or rows deleted 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 delete row 3. At the <TT>endloop</TT>,
row 4 becomes row 3, and so on. <TT>sy-tabix</TT> is incremented
by 0, giving 3. Row 4 was re-indexed to 3, so it is processed
on the next loop pass.
<H2><A NAME="CreatingTopListsUsingappendsortedby"><FONT SIZE=5 COLOR=#FF0000>
Creating Top 10 Lists Using append sorted by</FONT></A></H2>
<P>
Imagine that you are asked to create a report of the top 10 sales
representatives in your company. Assuming you have a way of obtaining
the total sales for each rep, you could do one of the following:
<UL>
<LI>Append all reps and their total sales into an internal table.
<LI>Sort them descending by sales.
<LI>Write out the first 10.
</UL>
<P>
This seems like a logical way to proceed. However, using the <TT>append
sorted by</TT> statement often can produce the same result and
be twice as efficient.
<H3><A NAME="SyntaxfortheappendsortedbyStatement">
Syntax for the append sorted by Statement</A></H3>
<P>
The following is the syntax for the <TT>append sorted by</TT>
statement.
<BLOCKQUOTE>
<PRE>
append [<I>wa</I> to] <I>it</I> sorted by <I>c</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT><I> </I>is
the name of an internal table.
<LI><TT><I>wa</I></TT> is a work
area having the same structure as a row of the internal table.
<LI><TT><I>c</I></TT> is a component
of <TT><I>it</I></TT>.
</UL>
<P>
The following points apply:
<UL>
<LI>If <TT><I>wa</I> to</TT> is
not specified, the row to be appended is taken from the header
line.
<LI>If <TT><I>wa</I> to</TT> is
specified, the row to be appended is taken from the work area
<TT><I>wa</I></TT>.
<LI>Only one component <TT><I>c</I></TT>
can be specified.
</UL>
<P>
The <TT>append sorted by</TT> statement takes a row from the work
area and inserts it into the internal table at the point where
it belongs in the sort order. It has two unusual properties:
<UL>
<LI>The number of rows that can be appended is limited by the
value on the <TT>occurs</TT> clause. For example, if the <TT>occurs</TT>
clause is 10, a maximum of 10 rows can be appended to the internal
table. This is the only situation where <TT>occurs</TT> limits
the number of rows that can be added to an internal table.
<LI>It only sorts in descending order.
</UL>
<P>
The net effect is a &quot;top <I>n</I> list,&quot; where <I>n</I>
is the number on the <TT>occurs</TT> clause.
<P>
With each append, the system searches the existing table contents
to determine where the new row fits. The sort order is by <TT><I>c</I></TT>
descending. If there are fewer rows in the internal table than
specified by <TT><I>n</I></TT>
on the <TT>occurs</TT> clause, the row is as per the sort order.
If there are <TT><I>n</I></TT>
rows in the internal table, the row is inserted as per the sort
order and the last row is discarded. If the value in <TT><I>c</I></TT>
already exists in the internal table, the new row is always appended
after existing rows that have the same value. Therefore, if <TT>occurs</TT>
is 3 and row 3 contains <TT>'X'</TT> in <TT><I>c</I></TT>,
a new row having <TT>'X'</TT> in <TT><I>c</I></TT>
will not be appended.
<P>
Listing 12.10 shows a sample program that creates a list of the
top three sales reps.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.10&nbsp;&nbsp;Using append sorted by to Find the
Top Three Sales Reps<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1  report ztx1210.
 2  data: begin of it occurs 3,
 3            sales type p decimals 2,
 4            name(10),
 5            end of it.
 6 
 7  it-sales     = 100.
 8  it-name      = 'Jack'.
 9  append it sorted by sales.
10
11 it-sales     = 50.
12 it-name      = 'Jim'.
13 append it sorted by sales.
14
15 it-sales     = 150.
16 it-name      = 'Jane'.
17 append it sorted by sales.
18
19 it-sales     = 75.
20 it-name      = 'George'.
21 append it sorted by sales.
22
23 it-sales     = 200.
24 it-name      = 'Gina'.
25 append it sorted by sales.
26
27 it-sales     = 100.
28 it-name      = 'Jeff'.
29 append it sorted by sales.
30
31 loop at it.
32     write: / it-sales, it-name.
33     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.10 produces this output:
<BLOCKQUOTE>
<PRE>
          200.00  Gina 
          150.00  Jane 
          100.00  Jack 
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 5 define an internal table with an <TT>occurs</TT>
value of <TT>3</TT>. If this internal table is filled using <TT>append
sorted by</TT>, the maximum number of rows in this internal table
will be limited to three.
<LI>Lines 7 and 8 assign values to the header line.
<LI>Line 9 searches <TT>it</TT> for the correct spot to insert
the new row. The internal table is empty, so the row is simply
appended (see Figure 12.1).<BR>
<A HREF="javascript:popUp('f12-1.gif')"><B>Figure 12.1 :</B> <I>No rows exist in the internal table, so
the first row is simply appended</I>.</A>
<LI>50 comes after 100 when sorting sales descending, so line
13 inserts the new row after the existing one (see Figure 12.2).
<BR>
<A HREF="javascript:popUp('f12-2.gif')"><B>Figure 12.2 :</B> <I>The second row is inserted in the correct
sort sequence so that it comes after the first row</I>.</A>
<LI>150 comes before 100, so line 15 inserts the new row before
the first row. The internal table now contains the maximum number
of rows: three rows (see Figure 12.3).<BR>
<A HREF="javascript:popUp('f12-3.gif')"><B>Figure 12.3 :</B> <I>This time the sort sequence dictates that
the new row be inserted before the first</I>.</A>
<LI>75 comes after 100, so line 21 inserts the new row after the
second row, thereby making the new row the third row. The third
row would become the fourth, but <TT>it</TT> can only hold three
rows, so the fourth is discarded (see Figure 12.4).<BR>
<A HREF="javascript:popUp('f12-4.gif')"><B>Figure 12.4 :</B> <I>This row fits after the second, so it is
inserted there. The internal table can hold a maximum of three
rows, so the fourth row is discarded</I>.</A>
<LI>200 comes before 150, so line 25 inserts it before the first
row. The rest of the rows are pushed down and the last is discarded
(see Figure 12.5).<BR>
<A HREF="javascript:popUp('f12-5.gif')"><B>Figure 12.5 :</B> <I>This time the new row should come first.
The existing rows are pushed down and the last one is discarded</I>.</A>
<LI>100 comes after 150, but there is already a 100 there. Line
29 therefore tries to insert the new row after the existing value
of 100. That would make it row 4, so the row is not inserted at
all (see Figure 12.6).<BR>
<A HREF="javascript:popUp('f12-6.gif')"><B>Figure 12.6 :</B> <I>This new row will follow all existing rows
that have the same value. That would make it row 4, so it is not
inserted</I>.</A>
</UL>
<P>
Do not mix <TT>append sorted by</TT> with any other statements
that add data to an internal table (such as <TT>insert</TT> or
<TT>append</TT>). If you fill <TT>it</TT> with <TT>append sorted
by</TT>, that should be the only statement you use. Mixing these
statements will result in unpredictable behavior.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
<TT>append sorted by</TT> is more efficient when you are appending one row at a time to an internal table. If you already have the data in a database table and merely want to find the top 10 values, it is more efficient to insert the rows using an array operation and then sort. (For information on array operations, see Day 13, &quot;Advanced Internal Tables: Part 2.&quot;)</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="FillinganInternalTableUsingcollect"><FONT SIZE=5 COLOR=#FF0000>
Filling an Internal Table Using collect</FONT></A></H2>
<P>
Using the <TT>collect</TT> statement, you can create totals within
an internal table as you fill it.
<H3><A NAME="SyntaxforthecollectStatement">
Syntax for the collect Statement</A></H3>
<P>
The following is the syntax for the <TT>collect</TT> statement.
<BLOCKQUOTE>
<PRE>
collect [<I>wa</I> into] <I>it</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is an internal
table.
<LI><TT><I>wa</I></TT> is a work
area that has the same structure as <TT><I>it</I></TT><I>.</I>
</UL>
<P>
The following points apply:
<UL>
<LI>If <TT><I>wa</I> into</TT>
is specified, the row to be collected is taken from the explicit
work area <TT><I>wa</I></TT>.
In this case, the header line of the internal table is ignored,
if it has one.
<LI>If <TT><I>wa</I> into</TT>
is not specified, the internal table must have a header line.
The row to be collected is taken from the header line for <TT><I>it</I></TT>.
</UL>
<P>
When <TT>collect</TT> is executed, the system forms a key from
the default key fields in the work area. The default key fields
are the character fields (types <TT>c</TT>, <TT>n</TT>, <TT>d</TT>,
<TT>t</TT>, and <TT>x</TT>). Therefore the key is composed of
the values from all fields of type <TT>c</TT>, <TT>n</TT>, <TT>d</TT>,
<TT>t</TT>, and <TT>x</TT>. It doesn't matter if they are beside
one another or separated from each other by other fields.
<P>
The system then searches the body of the internal table for a
row that has the same key as the key in the work area. If it doesn't
find one, the row is appended to the end of the table. If it does
find one, the numeric fields (types <TT>i</TT>, <TT>p</TT>, and
<TT>f</TT>) in the work area are added to the corresponding fields
in the found row. Listing 12.11 illustrates this concept.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.11&nbsp;&nbsp;collect Combines Rows as They Are
Added to an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1211.
2  data: begin of it occurs 10,
3            date type d,                    &quot;    part of d

⌨️ 快捷键说明

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