📄 ch10.htm
字号:
6 end of s1,
7 begin of s2,
8 x value 'X',
9 z value 'Z',
10 end of s2.
11
12 if s1-x = s2-x.
13 write: / s1-x, '=', s2-x.
14 else.
15 write: / s1-x, '<>', s2-x.
16 endif.
17
18 if s1-x between s2-x and s2-z.
19 write: / s1-X, 'is between', s2-x, 'and', s2-z.
20 else.
21 write: / s1-X, 'is not between', s2-x, 'and', s2-z.
22 endif.
23
24 if s1 = s2. "comparing field strings byte by byte
25 write: / 's1 = s2'.
26 else.
27 write: / 's1 <> s2'.
28 endif.
29
30 if 0 = ' '. "Watch out for this one
31 write: / '0 = '' '''.
32 else.
33 write: / '0 <> '' '''.
34 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.1 produces this output:
<BLOCKQUOTE>
<PRE>
X = X
X is between X and Z
s1 <> s2
0 = ' '
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>On line 12, <TT>s1-x</TT> is compared with <TT>s2-x</TT>.
Both are type <TT>c</TT>, length 1. No conversion is performed
and they are equal.
<LI>Line 18 is similar, but uses the <TT>between</TT> operator.
The value <TT>X</TT> lies in the range <TT>X</TT> to <TT>Z</TT>,
and so the test proves true.
<LI>Line 24 compares field strings <TT>s1</TT> and <TT>s2</TT>
as if they were type <TT>c</TT> variables. The value of <TT>s1</TT>
is therefore <TT>XYZ</TT>, and the value of <TT>s2</TT> is <TT>XZ</TT>.
They are not equal.
<LI>On line 30, the literal <TT>0</TT> is compared with a space.
The zero is stored internally as type <TT>i</TT>, the other as
type <TT>c</TT>. According to the order of precedence, type <TT>c</TT>
is converted to type <TT>i</TT>. Converting a blank to an integer
results in a zero value, and the comparison proves to be unexpectedly
true.
</UL>
<H3><A NAME="DisplayingConversions">
Displaying Conversions</A></H3>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
By performing a <I>program analysis,</I> you can determine where
conversions occur within a program. To perform a program analysis,
use the following procedure.
<P>
<img src="../button/screencam.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/screencam.gif">
<P>
Start the ScreenCam "How to Perform a Program Analysis"
now.
<OL>
<LI>Start at the ABAP/4 Editor: Initial Screen.
<LI>Type the program name to be analyzed in the Program field.
<LI>Choose the menu path Utilities->Program Analysis. The Conversions:
<I>xxxxx</I> screen is displayed.
<LI>Press the Conversions pushbutton. The Conversions: <I>xxxxx</I>
screen is displayed. All conversions are summarized on this screen.
The first column contains field types compared.
<LI>Double-click on any line to view it within the program.
</OL>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The first column on the Conversions: <I>xxxxx </I>screen does <I>not </I>show which conversion is performed. It merely shows the data types involved in the conversion.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="UsingTTFONTSIZEelseifFONTTT">
Using <TT><FONT SIZE=4>elseif</FONT></TT></A></H3>
<P>
You use <TT>elseif</TT> to avoid nesting <TT>if</TT>s. Nesting
<TT>if</TT>s can be difficult to read and maintain. (see Listing
10.2).
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.2 Using </B><TT><B>ELSEIF</B></TT><B>
Is Clearer than Using Nested </B><TT><B>IF</B></TT><B>s
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1002.
2 parameters: f1 default 'A',
3 f2 default 'B',
4 f3 default 'C'.
5
6 if f1 = f2. write: / f1, '=', f2.
7 elseif f1 = f3. write: / f1, '=', f3.
8 elseif f2 = f3. write: / f2, '=', f3.
9 else. write: / 'all fields are different'.
10 endif.
11
12 *lines 5-9 do the same as lines 14-26
13
14 if f1 = f2.
15 write: / f1, '=', f2.
16 else.
17 if f1 = f3.
18 write: / f1, '=', f3.
19 else.
20 if f2 = f3.
21 write: / f2, '=', f3.
22 else.
23 write: / 'all fields are different'.
24 endif.
25 endif.
26 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.2 produces this output:
<BLOCKQUOTE>
<PRE>
all fields are different
all fields are different
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>If <TT>f1</TT> = <TT>f2</TT>, line 6 is true.
<LI>If <TT>f1</TT> = <TT>f3</TT>, line 7 is true.
<LI>If <TT>f2</TT> = <TT>f3</TT>, line 8 is true.
<LI>If none of the above are true, line 9 is true.
<LI>Lines 14 through 26 do the same as lines 6 through 10. They
have been included so that you can see the two techniques side
by side.
</UL>
<H3><A NAME="UsingCharacterStringOperators">
Using Character String Operators</A></H3>
<P>
Special operators for character strings are shown in Table 10.3.
<BR>
<P>
<CENTER><B>Table 10.3 Special Operators for Character
Strings</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><BR>
<BR>
<CENTER><B>Operator</B></CENTER>
</TD><TD WIDTH=96><BR>
<BR>
<CENTER><B>Means</B></CENTER></TD>
<TD WIDTH=96><BR>
<BR>
<CENTER><B>True When</B></CENTER></TD>
<TD WIDTH=96><BR>
<CENTER><B>Case<BR>
Sensitive?</B></CENTER>
</TD><TD WIDTH=96><CENTER><B>Trailing<BR>
Blanks<BR>
Ignored?</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> CO <I>v2</I></TT>
</TD><TD WIDTH=96>Contains Only</TD><TD WIDTH=96><TT><I>v1</I></TT> is composed solely of characters in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><CENTER>Yes</TD><TD WIDTH=96><CENTER>No</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> CN <I>v2</I></TT>
</TD><TD WIDTH=96><TT>not <I>v1</I> CO <I>v2</I></TT>
</TD><TD WIDTH=96><TT><I>v1</I></TT> contains characters that are not in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><CENTER>Yes</TD><TD WIDTH=96><CENTER>No</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> CA <I>v2</I></TT>
</TD><TD WIDTH=96>Contains Any</TD><TD WIDTH=96><TT><I>v1</I></TT> contains at least one character in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><CENTER>Yes</TD><TD WIDTH=96><CENTER>No</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> NA <I>v2</I></TT>
</TD><TD WIDTH=96><TT>not <I>v1</I> CA <I>v2</I></TT>
</TD><TD WIDTH=96><TT><I>v1</I></TT> does not contain any character in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><CENTER>Yes</TD><TD WIDTH=96><CENTER>No</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> CS <I>v2</I></TT>
</TD><TD WIDTH=96>Contains String</TD><TD WIDTH=96><TT><I>v1</I></TT> contains the character string <TT><I>v2</I></TT>
</TD><TD WIDTH=96><center>No</TD><TD WIDTH=96><center>Yes</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> NS <I>v2</I></TT>
</TD><TD WIDTH=96><TT>not <I>v1</I> CS <I>v2</I></TT>
</TD><TD WIDTH=96><TT><I>v1</I></TT> does not contain the character string <TT><I>v2</I></TT>
</TD><TD WIDTH=96><center>No</TD><TD WIDTH=96><center>Yes</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> CP <I>v2</I></TT>
</TD><TD WIDTH=96>Contains Pattern</TD><TD WIDTH=96><TT><I>v1</I></TT> contains the pattern in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><center>No</TD><TD WIDTH=96><center>Yes</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT><I>v1</I> NP <I>v2</I></TT>
</TD><TD WIDTH=96><TT>not <I>v1</I> CP <I>v2</I></TT>
</TD><TD WIDTH=96><TT><I>v1</I></TT> does not contain the pattern in <TT><I>v2</I></TT>
</TD><TD WIDTH=96><center>No</TD><TD WIDTH=96><center>Yes</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
These operators can be used in any comparison expression. The
<TT>CS</TT>, <TT>NS</TT>, <TT>CP</TT>, and <TT>NP</TT> operators
ignore trailing blanks and are not case sensitive.
<P>
Although you can use variables, constants, or literals with relational
operators for character strings, for clarity Listing 10.3 only
uses literals. Also for clarity, the <TT>if</TT> statements here
occupy a single line each.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 10.3 A Sample Program Exercising </B><TT><B>CO</B></TT><B>,
</B><TT><B>CN</B></TT><B>, </B><TT><B>CA</B></TT><B>,
and </B><TT><B>NA<BR>
</B></TT>
<BLOCKQUOTE>
<PRE>
1 report ztx1003.
2 * operator: co
3 write / '''AABB'' co ''AB'''.
4 if 'AABB' co 'AB'. write 'True'. else. write 'False'. endif.
5 write / '''ABCD'' co ''ABC'''.
6 if 'ABCD' co 'ABC'. write 'True'. else. write 'False'. endif.
7
8 * operator: cn
9 write / '''AABB'' cn ''AB'''.
10 if 'AABB' cn 'AB'. write 'True'. else. write 'False'. endif.
11 write / '''ABCD'' cn ''ABC'''.
12 if 'ABCD' cn 'ABC'. write 'True'. else. write 'False'. endif.
13
14 * operator: ca
15 write / '''AXCZ'' ca ''AB'''.
16 if 'AXCZ' ca 'AB'. write 'True'. else. write 'False'. endif.
17 write / '''ABCD'' ca ''XYZ'''.
18 if 'ABCD' ca 'XYZ'. write 'True'. else. write 'False'. endif.
19
20 * operator: na
21 write / '''AXCZ'' na ''ABC'''.
22 if 'AXCZ' na 'ABC'. write 'True'. else. write 'False'. endif.
23 write / '''ABCD'' na ''XYZ'''.
24 if 'ABCD' na 'XYZ'. write 'True'. else. write 'False'. 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.3 produces this output:
<BLOCKQUOTE>
<PRE>
'AABB' co 'AB' True
'ABCD' co 'ABC' False
'AABB' cn 'AB' False
'ABCD' cn 'ABC' True
'AXCZ' ca 'AB' True
'ABCD' ca 'XYZ' False
'AXCZ' na 'ABC' False
'ABCD' na 'XYZ' True
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI><TT>co</TT> is the contains only operator. Line 4 is true
because <TT>'AABB'</TT> contains only characters from <TT>'AB'</TT>.
<LI>Line 6 is false because <TT>'ABCD'</TT> contains a <TT>D</TT>,
which is not in <TT>'ABC'</TT>.
<LI>Lines 10 and 12 are the opposites of lines 4 and 6 because
<TT>cn</TT> is the same as <TT>not <I>v1</I>
co <I>v2</I></TT>. Therefore,
the results are inverted. Line 10 is false and line 12 is true.
<LI><TT>ca</TT> is the contains any operator and line 16 is true
if <TT>'AXCZ'</TT> contains any characters from <TT>'AB'</TT>.
It is true because it contains <TT>A</TT>.
<LI>Line 18 is false because <TT>'ABCD'</TT> does not contain
any of the characters <TT>'XYZ'</TT>.
<LI><TT>na</TT> is equivalent to <TT>not v1 <I>ca</I>
v2</TT>. Therefore, lines 22 and 24 are the logical negation of
lines 16 and 18.
</UL>
<P>
Referring back to Table 10.3, the <TT>CP</TT> (contains pattern)
and <TT>NP</TT> (no pattern) operators perform a string search
that allows pattern-matching characters. The expression <TT><I>v1</I>
CP <I>v2</I></TT> is true when
<TT><I>v1</I></TT> contains a
string that matches the pattern in <TT><I>v2</I></TT>.
The expression <TT><I>v1</I> NP
<I>v2</I></TT> is true when <TT><I>v1</I></TT>
does not contain a string that matches the pattern in <TT><I>v2</I></TT>.
It is equivalent to <TT>not v1 cp v2</TT>. The pattern matching
characters allowed in <TT><I>v2</I></TT>
are given in Table 10.4.<BR>
<P>
<CENTER><B>Table 10.4 </B><TT><B>CP</B></TT><B>
and </B><TT><B>NP</B></TT><B>
Operators</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>Character</B></CENTER></TD><TD WIDTH=192><CENTER><B>Used to</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>*</TT></CENTER></TD><TD WIDTH=192>Match any sequence of characters.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>+</TT></CENTER></TD><TD WIDTH=192>Match any single character.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>#</TT></CENTER></TD><TD WIDTH=192>Interpret the next character literally.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<TT>#</TT> is the escape character. A single character following
it is interpreted exactly. Special meaning, if it exists, is lost.
You can also use <TT>#</TT> to make a search case sensitive or
to search for the <TT>*</TT>, <TT>+</TT>, or <TT>#</TT> characters.
Table 10.5 shows examples of how you might use these characters.
The escape character is needed when you want to perform a case-sensitive
search using <TT>CS</TT>, <TT>NS</TT>, <TT>CP</TT>, or <TT>NP</TT>.
You also need it if you want to perform a pattern search (<TT>CP</TT>
or <TT>NP</TT>) for a string containing <TT>*</TT>, <TT>+</TT>,
or <TT>#</TT>.<BR>
<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -