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

📄 ch13.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<LI>Within the loop that begins on line 5, the contents of the
row are written out first. They are then written again inside
of an <TT>at first</TT> and <TT>endat</TT>. The output shows that
the default key fields contain asterisks and the rest contain
zeros.
<LI>Line 13 assigns a new value to <TT>it-lifnr</TT>.
<LI>After the <TT>endat</TT> on line 14, the output shows that
all values have been restored to the work area. Changes to the
header line made inside of the <TT>at</TT> / <TT>endat</TT> are
lost.
<LI>Line 18 shows that <TT>at last</TT> exhibits the same behavior.
<LI>Line 27 exits the loop on the first pass to keep the output
simple.
</UL>
<H3><A NAME="UsingtheatnewandatendofStatements">
Using the at new and at end of Statements</A></H3>
<P>
Use the <TT>at new</TT> and <TT>at end of</TT> statements to detect
a change in a column from one loop pass to the next. These statements
enable you to execute code at the beginning and end of a group
of records.
<H4>Syntax for the at new and at end ff Statements</H4>
<P>
The following is the syntax for the <TT>at new</TT> and <TT>at
end of</TT> statements.
<BLOCKQUOTE>
<PRE>
sort by c.
loop at <I>it</I>.
    ---
    at new <I>c</I>.
        ---
        endat.
    ---
    at end of <I>c</I>.
        ---
        endat.
    ---
    endloop.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is an internal
table.
<LI><TT><I>c</I></TT> is a component
of <TT><I>it</I></TT><I>.</I>
<LI><TT>---</TT> represents any number of lines of code (even
zero).
</UL>
<P>
The following points apply:
<UL>
<LI>These statements can only be used within <TT>loop at</TT>;
they cannot be used within <TT>select</TT>.
<LI><TT>at new</TT> does not have to come before <TT>at end of</TT>.
These statements can appear in any order.
<LI>These statements can appear multiple times within the same
loop. For example, you could have two <TT>at new</TT> and three
<TT>at end of</TT> statements within one loop and they can appear
in any order.
<LI>These statements should not be nested inside of one another
(that is, <TT>at end of</TT> should not be placed inside of <TT>at
new</TT> / <TT>endat</TT>).
<LI>There are no additions to these statements.
</UL>
<H4>Using at new</H4>
<P>
Each time the value of <TT><I>c</I></TT>
changes, the lines of code between <TT>at new</TT> and <TT>endat</TT>
are executed. This block is also executed during the first loop
pass or if any fields to the left of <TT><I>c</I></TT>
change. Between <TT>at</TT> and <TT>endat</TT>, the numeric fields
to the right of <TT><I>c</I></TT>
are set to zero. The non-numeric fields are filled with asterisks
(<TT>*</TT>). If there are multiple occurrences of <TT>at new</TT>,
they are all executed. <TT>at end of</TT> behaves in a similar
fashion.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>control level</I> is the component named on a control break
statement; it regulates the control break. For example, in the
following code snippet, <TT>f2</TT> is a control level because
it appears on the <TT>at new</TT> statement.
<BLOCKQUOTE>
<PRE>
loop at it.
    at new f2.
        &quot;(some code here)
        endat.
    endloop.
</PRE>
</BLOCKQUOTE>
<P>
It is said that a control break is triggered if the control level
changes. This means that when the contents of the control level
change, the code between the <TT>at</TT> and <TT>endat</TT> is
executed.
<P>
A control break is also triggered if any of the fields prior to
the control level in the structure change. Therefore, you should
define the internal table structure to begin with the fields that
form your control levels. You must also sort by all fields prior
to and including <TT><I>c</I></TT>.
<P>
Between <TT>at</TT> and <TT>endat</TT>, numeric fields to the
right of the control level will be zero and non-numeric fields
will be filled with asterisks.
<P>
Figures 13.7 and 13.8 illustrate the use of <TT>at new</TT>.
<P>
<A HREF="javascript:popUp('f13-7.gif')"><B>Figure 13.7 :</B> <I>This figure shows that the control level
is triggered when the value of f1 changes. It also shows that
f2 contains an asterisk within the control break (between at and
endat)</I>.</A>
<P>
<A HREF="javascript:popUp('f13-8.gif')"><B>Figure 13.8 :</B> <I>This figure shows that the control level
is triggered when f2 or f1 changes. This happens because f1 is
prior to f2 in the structure of it</I>.</A>
<P>
This program provides a working example of Figures 13.7 and 13.8.
It illustrates the point that a control break is triggered whenever
the control level changes or any field prior to the control level
changes.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.9&nbsp;&nbsp;The Code in Figures 13.7 and 13.8 Is
Reproduced in a Working Example<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1 report ztx1309.
 2 data: begin of it occurs 4,
 3           f1,
4            f2,
5            end of it.
6 
7  it = '1A'. append it. &quot;Fill it with data
8  it = '3A'. append it.
9  it = '1B'. append it.
10 it = '2B'. append it.
11
12 sort it by f1.        &quot;it now looks like figure 13.7
13 loop at it.
14     at new f1.
15         write: / it-f1, it-f2.
16         endat.
17     endloop.
18 skip.
19 sort it by f2.        &quot;it now looks like figure 13.8
20 loop at it.
21     at new f2.
22         write: / it-f1, it-f2.
23         endat.
24     endloop.
25 skip.
26 sort it by f1 f2.     &quot;it now looks like figure 13.8
27 loop at it.
28     at new f1.
29         write: / it-f1.
30         endat.
31     at new f2.
32         write: /4 it-f2.
33         endat.
34     endloop.
35 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 13.9 produces this output:
<BLOCKQUOTE>
<PRE>
   1 *
   2 *    
   3 *    
          
   1 A    
   3 A    
   1 B    
   2 B    
          
   1      
      A   
      B   
   2      
      B   
   3      
      A   
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 12 sorts <TT>it</TT> by <TT>f1</TT>. On line 14, <TT>at
new</TT> is triggered the first time through the loop and each
time <TT>f1</TT> changes.
<LI>Line 19 sorts <TT>it</TT> by <TT>f2</TT>. On line 20, <TT>at
new</TT> is triggered the first time through or each time the
value of <TT>f1</TT> or <TT>f2</TT> changes. This happens because
a control level is triggered each time <TT>f2</TT> changes or
any field prior to <TT>f2</TT> changes.
<LI>Line 26 sorts <TT>it</TT> by both <TT>f1</TT> and <TT>f2</TT>.
On line 28, <TT>at new</TT> is triggered each time the value of
<TT>f1</TT> changes, and on line 31, <TT>at new</TT> is triggered
each time <TT>f1</TT> or <TT>f2</TT> changes.
</UL>
<H4>Using at end of</H4>
<P>
The lines of code between <TT>at end of</TT> and <TT>endat</TT>
are executed:
<UL>
<LI>If the control level changes.
<LI>If any field prior to the control level changes.
<LI>If this is the last row of the table.
</UL>
<P>
Listing 13.10 illustrates this statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.10&nbsp;&nbsp;How the at new and at end of Are Triggered
by the Field Changes and Control Level Changes<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1310.
2  data: begin of it occurs 4,
3            f1,
4            f2,
5            end of it.
6 
7  it = '1A'. append it. &quot;Fill it with data
8  it = '3A'. append it.
9  it = '1B'. append it.
10 it = '2B'. append it.
11
12 sort it by f1.
13 loop at it.
14     at new f1.
15         write: / 'start of:', it-f1.
16         endat.
17     write: /4 it-f1.
18     at end of f1.
19         write: / 'end   of:', it-f1.
20         endat.
21     endloop.
22 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 13.10 produces this output:
<BLOCKQUOTE>
<PRE>
start of: 1  
     1         
     1         
  end   of: 1  
  start of: 2  
     2         
  end   of: 2  
  start of: 3  
     3         
  end   of: 3  
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 12 sorts <TT>it</TT> by <TT>f1</TT>.
<LI>On line 14, <TT>at new</TT> is triggered each time <TT>f1</TT>
changes.
<LI>On line 18, <TT>at end of</TT> is triggered if the control
level will change when the next row is read or if this is the
last row.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Do not use control break statements within a <TT>loop at <I>it</I> where </TT>. . . con-struct; the effects are unpredictable.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<H3><A NAME="UsingthesumStatement">
Using the sum Statement</A></H3>
<P>
Use the <TT>sum</TT> statement to calculate totals for the rows
of a control level.
<H4>Syntax for the sum Statement</H4>
<P>
The following is the syntax for the <TT>sum</TT> statement.
<BLOCKQUOTE>
<PRE>
at first/last/new/end of.
    ---
    sum.
    ---
    endat.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT>---</TT> represents any number of lines of code.
</UL>
<P>
<TT>Sum</TT> calculates a total for the current value of the control
level that contains it. This is clearer if you imagine that it
does the following:
<UL>
<LI>It finds all rows that have the same values within the control
level field and all fields to the left of it.
<LI>It sums each numeric column to the right of the control level.
<LI>It places the totals in the corresponding fields of the work
area.
</UL>
<P>
Listing 13.11 illustrates this statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.11&nbsp;&nbsp;Using the sum Statement to Calculate
the Totals of a Control Level<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1311.
2  data: begin of it occurs 4,
3            f1,
4            f2 type i,
5            f3 type i,
6            end of it.
7 
8  it-f1 = 'A'. it-f2 = 1. it-f3 = 10. append it.
9  it-f1 = 'B'. it-f2 = 3. it-f3 = 30. append it.
10 it-f1 = 'A'. it-f2 = 2. it-f3 = 20. append it.
11 it-f1 = 'B'. it-f2 = 4. it-f3 = 40. append it.
12
13 sort it by f1.        &quot;it now looks like figure 13.9
14 loop at it.
15     at new f1.
16         sum.
17         write: / 'total:', it-f1, it-f2, it-f3.
18         endat.
19     write: /11 it-f2, it-f3.
20     endloop.
21 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 13.11 produces this output:
<BLOCKQUOTE>

⌨️ 快捷键说明

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