📄 ch10.htm
字号:
<CENTER><B>Table 10.5 Using Characters</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Statement</B></CENTER></TD><TD WIDTH=288><CENTER><B>True When</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><TT><I>v1</I> CP 'A+C'</TT></CENTER>
</TD><TD WIDTH=288><TT><I>v1</I></TT> contains "a" in the first position and "c" in the third. Either character can be in upper- or lowercase. Any character can appear in the second position.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><TT><I>v1</I> CP '*Ab*'</TT></CENTER>
</TD><TD WIDTH=288>The string "ab" occurs anywhere within <TT><I>v1</I></TT>. Either character can be in upper- or in lowercase.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><TT><I>v1</I> CP '*#A#b*'</TT></CENTER>
</TD><TD WIDTH=288><TT><I>v1</I></TT> contains a capital A followed by lowercase b.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><TT><I>v1</I> CP '*##*'</TT></CENTER>
</TD><TD WIDTH=288><TT><I>v1</I></TT> contains a <TT>#</TT>.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Use of these operators always sets the system variable <TT>sy-fdpos</TT>.
If the result of the comparison is true, <TT>sy-fdpos</TT> contains
the zero-based offset of the first matching or non-matching character.
Otherwise, <TT>sy-fdpos</TT> contains the length of <TT><I>v1</I></TT>.
The value assigned to <TT>sy-fdpos</TT> by each operator is described
in Table 10.6.
<P>
After a comparison, <TT>sy-fdpos</TT> will contain either a 1st
character offset or the length of v1. In this table, "1st
char(v1)" means "the offset of the first character of
(the string or pattern) v1 that is." So, for example, where
you see "1st char(v1) in v2," read that as "the
zero-based offset of the first character of v1 that is also in
v2." Length(v1) means "the length of v1." Note
that only the first column contains ABAP/4 statements.<BR>
<P>
<CENTER><B>Table 10.6 Value Assigned to </B><TT><B>SY-FDPOS</B></TT><B>
by Each Operator</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><B>Comparison</B></CENTER></TD><TD WIDTH=144><TT><B>if TRUE sy-fdpos =</B></TT>
</TD><TD WIDTH=240><TT><B>if FALSE sy-fdpos =</B></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> CO <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>length (<TT><I>v1</I></TT>)
</TD><TD WIDTH=240>1stchar (<TT><I>v1</I></TT>) not in <TT><I>v2</I></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> CN <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>1stchar (<TT><I>v1</I></TT>) in <TT><I>v2</I></TT>
</TD><TD WIDTH=240>length (<TT><I>v1</I></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> CA <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>1stchar (<TT><I>v1</I></TT>) in <TT><I>v2</I></TT>
</TD><TD WIDTH=240>length (<TT><I>v1</I></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> NA <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>length (<TT><I>v1</I></TT>)
</TD><TD WIDTH=240>1stchar (<TT><I>v1</I></TT>) in <TT><I>v2</I></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> CS <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>1stchar (<TT><I>v2</I></TT>) in <TT><I>v1</I></TT>
</TD><TD WIDTH=240>length (<TT><I>v1</I></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> NS <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>length (<TT><I>v1</I></TT>)
</TD><TD WIDTH=240>1stchar (<TT><I>v1</I></TT>) in <TT><I>v2</I></TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=144><CENTER><TT><I>v1</I> CP <I>v2</I></TT></CENTER>
</TD><TD WIDTH=144>1stchar (<TT><I>v2</I></TT>) in <TT><I>v1</I></TT>
</TD><TD WIDTH=240>length (<TT><I>v1</I></TT>)
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 10.4 is similar to Listing 10.3, but lines have been added
to display the value of <TT>sy-fdpos</TT> after each comparison.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.4 Using </B><TT><B>SY-FDPOS</B></TT><B>
to See the Offset of the First Matching or Non-matching Character
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1004.
2 * operator: co
3 write / '''AABB'' co ''AB'''.
4 if 'AABB' co 'AB'. write 'True'. else. write 'False'. endif.
5 write: 'sy-fdpos=', sy-fdpos.
6 write / '''ABCD'' co ''ABC'''.
7 if 'ABCD' co 'ABC'. write 'True'. else. write 'False'. endif.
8 write: 'sy-fdpos=', sy-fdpos.
9
10 * operator: cn
11 write / '''AABB'' cn ''AB'''.
12 if 'AABB' cn 'AB'. write 'True'. else. write 'False'. endif.
13 write: 'sy-fdpos=', sy-fdpos.
14 write / '''ABCD'' cn ''ABC'''.
15 if 'ABCD' cn 'ABC'. write 'True'. else. write 'False'. endif.
16 write: 'sy-fdpos=', sy-fdpos.
17
18 * operator: ca
19 write / '''AXCZ'' ca ''AB'''.
20 if 'AXCZ' ca 'AB'. write 'True'. else. write 'False'. endif.
21 write: 'sy-fdpos=', sy-fdpos.
22
23 write / '''ABCD'' ca ''XYZ'''.
24 if 'ABCD' ca 'XYZ'. write 'True'. else. write 'False'. endif.
25 write: 'sy-fdpos=', sy-fdpos.
26
27
28 * operator: na
29 write / '''AXCZ'' na ''ABC'''.
30 if 'AXCZ' na 'ABC'. write 'True'. else. write 'False'. endif.
31 write: 'sy-fdpos=', sy-fdpos.
32
33 write / '''ABCD'' na ''XYZ'''.
34 if 'ABCD' na 'XYZ'. write 'True'. else. write 'False'. endif.
35 write: 'sy-fdpos=', sy-fdpos.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.4 produces this output:
<BLOCKQUOTE>
<PRE>
'AABB' co 'AB' True sy-fdpos = 4
'ABCD' co 'ABC' False sy-fdpos = 3
'AABB' cn 'AB' False sy-fdpos = 4
'ABCD' cn 'ABC' True sy-fdpos = 3
'AXCZ' ca 'AB' True sy-fdpos = 0
'ABCD' ca 'XYZ' False sy-fdpos = 4
'AXCZ' na 'ABC' False sy-fdpos = 0
'ABCD' na 'XYZ' True sy-fdpos = 4
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 4, a <TT>co</TT> comparison, is true. Therefore, <TT>sy-fdpos</TT>
contains 4, the length of the first operand.
<LI>Line 7, also a <TT>co</TT>, is false because <TT><I>v1</I></TT>
contains values not in <TT><I>v2</I></TT>.
<TT>Sy-fdpos</TT> contains <TT>3</TT>, the zero-based offset of
the first character in <TT><I>v1</I></TT>
that is not in <TT><I>v2</I></TT>.
<LI>Although lines 12 and 15 are the logical opposites of lines
4 and 7, the values of <TT>sy-fdpos</TT> do not change.
<LI>Line 20, a <TT>ca</TT> comparison, is true because <TT><I>v1</I></TT>
contains a character from <TT><I>v2</I></TT>.
<TT>Sy-fdpos</TT> contains <TT>0</TT>: the zero-based offset of
the first character in <TT><I>v1</I></TT>
that is also in <TT><I>v2</I></TT>.
<LI>On line 24, <TT><I>v1</I></TT>
does not contain any of the characters in <TT><I>v2</I></TT>,
therefore <TT>sy-fdpos</TT> contains <TT>4</TT>: the length of
<TT><I>v1</I></TT>.
<LI>Line 30 is the logical negation of a <TT>ca</TT> test. <TT>'AXCZ'</TT>
does contain a value from <TT>'ABC'</TT> and so a <TT>ca</TT>
test would be true. Negating this result gives false. <TT>Sy-fdpos</TT>
is set to the zero-based offset of the first character in <TT>'AXCZ'</TT>
that is also in <TT>'ABC'</TT>.
<LI>Line 34 is, again, a logical negation of a <TT>ca</TT> test.
<TT>'ABCD'</TT> does not contain a value from <TT>'XYZ'</TT> and
so a <TT>ca</TT> test would be false. Negating this result yields
true. <TT>Sy-fdpos</TT> is set to the length of <TT>'ABCD'</TT>.
</UL>
<H2><A NAME="UsingtheTTFONTSIZEcaseFONTTTFONTSIZEStatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>case</FONT></TT><FONT SIZE=5> Statement</FONT>
</FONT></A></H2>
<P>
The <TT>case</TT> statement performs a series of comparisons.
<H3><A NAME="SyntaxfortheTTFONTSIZEcaseFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>case</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following is the syntax for the <TT>case</TT> statement.
<BLOCKQUOTE>
<PRE>
case <I>v1</I>.
when <I>v2</I> [ or <I>vn</I> ... ].
<I>---
</I> when <I>v3</I> [ or <I>vn</I> ... ].
<I>---
</I> [ when others.
<I>---</I> ]
endcase.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>v1</I></TT><I> </I>or<I>
</I><TT><I>v2</I></TT> can be
a variable, literal, constant, or field string.
<LI><TT><I>---</I></TT> represents
any number of lines of code. Even zero lines are allowed.
</UL>
<P>
The following points apply:
<UL>
<LI>Only statements following the first matching <TT>when</TT>
are executed.
<LI><TT>when others</TT> matches if none of the preceding <TT>when</TT>s
match.
<LI>If <TT>when others</TT> is not coded and none of the <TT>when</TT>s
match, processing continues with the first statement following
<TT>endcase</TT>.
<LI>Expressions are not allowed.
<LI>Field strings are treated as type <TT>c</TT> variables.
</UL>
<P>
<TT>Case</TT> is very similar to <TT>if</TT>/<TT>elseif</TT>.
The only difference is that on each <TT>if</TT>/<TT>elseif</TT>,
you can specify a complex expression. With <TT>case</TT>, you
can specify only a single value to be compared, and values are
always compared for equality. An example is given in Listing 10.5.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.5 The </B><TT><B>CASE</B></TT><B>
Statement Performs a Series of Comparisons<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1005.
2 parameters f1 type i default 2.
3
4 case f1.
5 when 1. write / 'f1 = 1'.
6 when 2. write / 'f1 = 2'.
7 when 3. write / 'f1 = 3'.
8 when others. write / 'f1 is not 1, 2, or 3'.
9 endcase.
10
11 * The following code is equivalent to the above case statement
12 if f1 = 1. write / 'f1 = 1'.
13 elseif f1 = 2. write / 'f1 = 2'.
14 elseif f1 = 3. write / 'f1 = 3'.
15 else. write / 'f1 is not 1, 2, or 3'.
16 endif.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.5 produces this output:
<BLOCKQUOTE>
<PRE>
f1 = 2
f1 = 2
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 defines <TT>f1</TT> as a single character parameter
having a default value <TT>2</TT>.
<LI>On line 4, the <TT>case</TT> statement begins and <TT>f1</TT>
is compared with 1, 2, and 3, in sequence.
<LI>Line 6 matches and executes the <TT>write</TT> statement that
immediately follows it. The remaining <TT>when</TT> statements
are ignored.
<LI>The next line executed is line 12.
</UL>
<H2><A NAME="UsingtheTTFONTSIZEexitFONTTTFONTSIZEStatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>exit</FONT></TT><FONT SIZE=5> Statement</FONT>
</FONT></A></H2>
<P>
The <TT>exit</TT> statement prevents further processing from occurring.
<H3><A NAME="SyntaxfortheTTFONTSIZEexitFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>exit</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<BLOCKQUOTE>
<PRE>
exit.
</PRE>
</BLOCKQUOTE>
<P>
Listing 10.6 shows a sample program using <TT>exit</TT>.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.6 Using </B><TT><B>EXIT</B></TT><B>
to Stop Program Processing Dead in Its Tracks<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1006.
2 write: / 'Hi'.
3 exit.
4 write: / 'There'.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 10.6 produces this output:
<BLOCKQUOTE>
<PRE>
Hi
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Exit prevents further processing, so the <TT>exit</TT> on line
3 prevents line 4 from being executed.
<P>
<TT>exit</TT> can be used in many situations. It can have varying
effects depending on where it appears in the code. However, it
always prevents further processing. Within loop structures, it
leaves loop processing introduced by statements such as <TT>loop</TT>,
<TT>select</TT>, <TT>do</TT>, and <TT>while</TT>. Within subroutines,
it leaves subroutines introduced by FORM. Its effects in other
situations will be fully explored as they arise.
<H2><A NAME="UsingtheTTFONTSIZEdoFONTTTFONTSIZEStatementFONT"><FONT SIZE=5 COLOR=#FF0000>
Using the <TT><FONT SIZE=5>do</FONT></TT><FONT SIZE=5> Statement</FONT>
</FONT></A></H2>
<P>
The <TT>do</TT> statement is a basic loop mechanism.
<H3><A NAME="SyntaxfortheTTFONTSIZEdoFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>do</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following is the syntax for the <TT>do</TT> statement.
<BLOCKQUOTE>
<PRE>
do [ <I>v1</I> times ] [ varying <I>f1</I> from <I>s-c1</I> next <I>s-c2</I> [ varying <I>f2 </I>from <I>s2-c1</I> next <I>s2-c2</I> ... ] ].
---
[exit.]
---
enddo.
</PRE>
</BLOCKQUOTE>
<P>
where:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -