📄 ch09.htm
字号:
than Blanks or Zeros Using the </B><TT><B>WITH</B></TT><B>
Addition of the </B><TT><B>CLEAR</B></TT><B>
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0905.
2 tables ztxlfa1.
3 data: f1(2) type c value 'AB',
4 f2(2) type c,
5 f3 type i value 12345,
6 begin of s1,
7 f1(3) type c value 'XYZ',
8 f2 type i value 123456,
9 end of s1.
10 write: / 'f1=''' no-gap, f1 no-gap, '''',
11 / 'f2=''' no-gap, f2 no-gap, '''',
12 / 'f3=''' no-gap, f3 no-gap, '''',
13 / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
14 / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
15 / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
16 / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, '''',
17 /.
18 clear: f1 with 'X',
19 f2 with f1,
20 f3 with 3,
21 s1 with 'X',
22 ztxlfa1 with 0.
23 write: / 'f1=''' no-gap, f1 no-gap, '''',
24 / 'f2=''' no-gap, f2 no-gap, '''',
25 / 'f3=''' no-gap, f3 no-gap, '''',
26 / 's1-f1=''' no-gap, s1-f1 no-gap, '''',
27 / 's1-f2=''' no-gap, s1-f2 no-gap, '''',
28 / 'ztxlfa1-lifnr=''' no-gap, ztxlfa1-lifnr no-gap, '''',
29 / 'ztxlfa1-land1=''' no-gap, ztxlfa1-land1 no-gap, ''''.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 9.5 produces this output:
<BLOCKQUOTE>
<PRE>
f1='AB'
f2=' '
f3=' 12,345 '
s1-f1='XYZ'
s1-f2=' 123,456 '
ztxlfa1-lifnr=' '
ztxlfa1-land1=' '
f1='XX'
f2='XX'
f3='50,529,027 '
s1-f1='XXX'
s1-f2='1482184792 '
ztxlfa1-lifnr='##########'
ztxlfa1-land1='###'<BR>
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 18 fills <TT>f1</TT> with the letter <TT>X</TT>.
<LI>Line 19 fills <TT>f2</TT> with the first byte of <TT>f1</TT>,
also an <TT>X</TT>.
<LI>Line 20 fills <TT>f3</TT> with the first byte of the literal
<TT>3</TT>. A numeric literal up to nine digits long is stored
as a four-byte integer (see the following section "Data Conversion").
<TT>f3</TT> is filled with the first byte of this four-byte integer,
essentially assigning garbage to <TT>f3</TT>.
<LI>Line 21 treats <TT>s1</TT> as a type <TT>c</TT> variable and
fills it with <TT>X</TT>. Component <TT>f1</TT> is type <TT>c</TT>,
so it receives valid values. Component <TT>f2</TT> is type <TT>i</TT>
and so receives invalid values.
<LI>Field string <TT>ztxlfa1</TT> is filled with the first byte
from the four-byte integer value 0, filling it with garbage. Garbage,
in this case, is displayed as hash marks (<TT>#</TT>).
</UL>
<H3><A NAME="UsingtheTTFONTSIZEmoveFONTTTFONTSIZEStatementFONT">
Using the <TT><FONT SIZE=4>move</FONT></TT><FONT SIZE=4> Statement</FONT>
</A></H3>
<P>
To move a value from one field to another, use the <TT>move</TT>
statement. The entire contents or a portion thereof can be moved.
Instead of <TT>move</TT>, you can use the assignment operator
<TT>=</TT>, as shown below. They are both referred to as a <TT>move</TT>
statement.
<H4>Syntax for the <TT>move</TT> Statement</H4>
<P>
The following is the syntax for the <TT>move</TT> statement. Operators
and operands must be separated by spaces. Multiple assignment
occurs from right to left.
<BLOCKQUOTE>
<PRE>
move <I>v1</I> to <I>v2</I>.
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
<I>v2</I> = <I>v1</I>.
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
<I>v2</I> = <I>v1</I> = <I>vm</I> = <I>vn</I> . . ..
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
move <I>v1</I>[+<I>N</I>(<I>L</I>)] to <I>v2</I>[+<I>N</I>(<I>L</I>)].
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
<I>v2</I>[+<I>N</I>(<I>L</I>)] = <I>v1</I>[+<I>N</I>(<I>L</I>)].
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>v1</I></TT> is the
sending variable or field string.
<LI><TT><I>v2</I></TT> is the
receiving variable or field string.
<LI><TT><I>N</I></TT> is an offset
from the beginning of the variable or field string.
<LI><TT><I>L</I></TT> is the number
of bytes to move.
</UL>
<P>
These are two examples in Table 9.2 of the right and wrong ways
to code assignment statements. Incorrect coding results in a syntax
error.<BR>
<P>
<CENTER><B>Table 9.2 Right and Wrong Coding of Assignment
</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>Right</B></CENTER></TD><TD WIDTH=96><CENTER><B>Wrong</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>f1 = f2.</TT></TD><TD WIDTH=96><TT>f1=f2.</TT>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>f1 = f2 = f3.</TT></TD><TD WIDTH=96><TT>f1=f2=f3.</TT>
</TD></TR>
</TABLE>
</CENTER>
<H4>Data Conversions</H4>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
If two variables have different data types or lengths, the data
is converted when it is moved. This is called an <I>automatic
adjustment</I>. If the lengths of the sending and receiving variables
do not match, an automatic <I>length</I> adjustment is performed.
If the data types do not match, an automatic <I>type</I> adjustment
is performed.
<P>
If the data types of the sending and receiving fields are the
same but the lengths differ, a length adjustment is performed
as shown in Table 9.3. In this table, the sending field is the
"From" field.<BR>
<P>
<CENTER><B>Table 9.3 Effect of Length Adjustment Varies
with the Data Type</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>Type</B></CENTER></TD><TD WIDTH=192><B>When assigning to a longer field, the <TT><B>'from' </B></TT>value is:</B>
</TD><TD WIDTH=192><B>When assigning to a shorter field, the <TT><B>'from' </B></TT>value is:</B>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=192>Right-padded with blanks
</TD><TD WIDTH=192>Right-truncated</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>x</TT></CENTER></TD><TD WIDTH=192>Right-padded with zeros
</TD><TD WIDTH=192>Right-truncated</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>n</TT></CENTER></TD><TD WIDTH=192>Left-padded with zeros
</TD><TD WIDTH=192>Left-truncated</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>p</TT></CENTER></TD><TD WIDTH=192>Left-padded with zeros
</TD><TD WIDTH=192>Assigned if the numeric value will fit in the <TT>'to'</TT> field.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER> </CENTER></TD><TD WIDTH=192>
</TD><TD WIDTH=192>If the numeric value is too large for the receiving field, a short dump occurs.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The remaining data types (<TT>f</TT>, <TT>i</TT>, <TT>d</TT>,
and <TT>t</TT>) are all of a fixed length, so the sending and
receiving fields will always be the same length if they are of
the same data type.
<P>
Rules for type adjustments are provided in Table 9.4. The conversion
rules for type <TT>i</TT> are the same as for type <TT>p</TT>.
Included are conversions with unusual behaviors. Points to notice
are:
<UL>
<LI>The peculiar compression performed for type <TT>c</TT> to
<TT>n</TT>.
<LI>The capability to assign invalid values to types <TT>d</TT>
and <TT>t</TT>.
<LI>The odd treatment of invalid characters during conversion
of type <TT>c</TT> to <TT>x</TT>.
<LI>The unexpected usage of the reserved sign byte in <TT>p</TT>
to <TT>c</TT> conversions.
<LI>The use of <TT>*</TT> to indicate overflow in <TT>p</TT> to
<TT>c</TT> conversions.
<LI>An entirely blank <TT>c</TT> field is converted to a <TT>p</TT>
field having a zero value.
</UL>
<P>
<P>
<CENTER><B>Table 9.4 Rules for Type Adjustments</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>From Type</B></CENTER></TD><TD WIDTH=96><CENTER><B>To Type</B></CENTER>
</TD><TD WIDTH=288><CENTER><B>Conversion Rules</B></CENTER></TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>p</TT></CENTER>
</TD><TD WIDTH=288>The sending field can only contain numbers, a single decimal point, and an optional sign. The sign can be leading or trailing. Blanks can appear on either side of the value. It is right-justified and padded on the left with zeros. An entirely blank sending field is converted to zero.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>d</TT></CENTER>
</TD><TD WIDTH=288>The sending field should contain only a valid date in the format YYYYMMDD. If it does not, an error does not occur; instead, an invalid value is assigned to the receiving field. The results of using this value are undefined.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>t</TT></CENTER>
</TD><TD WIDTH=288>The sending field should only have a valid time in the format HHMMSS. If it does not, an error does not occur; instead, an invalid value is assigned to the receiving field. The results of using this value are undefined.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>n</TT></CENTER>
</TD><TD WIDTH=288>The sending field is scanned from left to right and only the digits <TT>0</TT>-<TT>9</TT> are transferred to the receiving field (right-justified) and padded on the left with zeros. All other characters are simply ignored.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>x</TT></CENTER>
</TD><TD WIDTH=288>Valid values for the sending field are <TT>0</TT>-<TT>9</TT> and capital letters <TT>A</TT>-<TT>F</TT>. The value is left-justified and padded on the right with zeros or truncated on the right. All characters after the first invalid value in the sending field are ignored.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>p</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>c</TT></CENTER>
</TD><TD WIDTH=288>The value is right-justified in the receiving field with the right-most byte reserved for a trailing sign. The sign is only displayed if the number is negative; therefore, positive numbers will be right-justified with a single trailing blank. In the event you try to move a positive value that contains as many digits as the receiving field is long, the system will use the entire length of the receiving field to contain the value without reserving the right-most byte for the sign. After considering the above, if the value in the sending field will not fit the receiving field, the number is truncated on the left. If truncation has occurred, the system indicates this by replacing the left-most digit with an asterisk (<TT>*</TT>). If the value does fit in the receiving field, leading zeros are suppressed. If the sending field is equal to zero, the receiving field receives a single zero.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>p</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>d</TT></CENTER>
</TD><TD WIDTH=288>The number is interpreted as the number of days since 0001/01/01, converted to a date, and stored internally in YYYYMMDD format.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>p</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>t</TT></CENTER>
</TD><TD WIDTH=288>The number is interpreted as the number of seconds since midnight, converted to 24-hour clock time, and stored internally in HHMMSS format.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>d</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>p</TT></CENTER>
</TD><TD WIDTH=288>The date is converted to a number representing the number of days since 0001/01/01.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><TT>t</TT></CENTER></TD><TD WIDTH=96><CENTER><TT>p</TT></CENTER>
</TD><TD WIDTH=288>The time is converted to a number representing the number of seconds since midnight.
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
For a complete list of conversion rules, consult the ABAP/4 keyword
documentation for the <TT>move</TT> statement. The procedure for
displaying it follows in the next section.
<P>
Listing 9.6 contains a demonstration program that performs sample
data conversions.
<P>
<img src="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 9.6 Sample Data Conversions<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0906.
2 constants >(3) value '==>'. "defines a constant named '>'
3 data: fc(10) type c value '-A1B2C3.4',
4 fn(10) type n,
5 fp type p,
6 fd type d,
7 ft type t,
8 fx(4) type x,
9 fc1(5) type c value '-1234',
10 fc2(5) type c value '1234-',
11 fp1 type p value 123456789,
12 fp2 type p value '123456789-',
13 fp3 type p value 1234567899,
14 fp4 type p value 12345678901,
15 fp5 type p value 12345,
16 fp6 type p value 0.
17
18 fn = fc. write: / fc, >, fn, 'non-numeric chars are ignored'.
19 fd = 'ABCDE'. write: / fd, 'date and time fields are invalid'.
20 ft = 'ABCDE'. write: / ft, ' when you load them with junk'.
21 fp = sy-datum. write: / sy-datum, >, fp, 'd->p: days since 0001/01/01'.
22 fp = sy-uzeit. write: / sy-uzeit, >, fp, 'd->t: secs since midnight'.
23 fx = 'A4 B4'. write: / 'A4 B4', >, fx, 'ignore all after invalid char'.
24 fp = fc1. write: / fc1, >, fp, 'allows leading sign'.
25 fp = fc2. write: / fc2, >, fp, 'also allows trailing sign'.
26 fc = fp1. write: / fp1, >, fc, 'rightmost byte reserved for sign'.
27 fc = fp2. write: / fp2, >, fc, 'only negative numbers use it, but'.
28 fc = fp3. write: / fp3, >, fc, '+ve nums that need it use it too'.
29 fc = fp4. write: / fp4, >, fc, 'overflow indicated by leading *'.
30 fc = fp5. write: / fp5, >, fc, 'leading zeros are suppressed'.
31 fc = fp6. write: / fp6, >, fc, 'zero in = zero out'.
32 fp = ' '. write: / ' ', >, fp, 'blanks in = zero out'.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 9.6 produces the following output:
<BLOCKQUOTE>
<PRE>
-A1B2C3.4 ==> 0000001234 non-numeric chars are ignored
E ABCD date and time fields are invalid
ABCDE0 when you load them with junk
1998/02/22 ==> 729,443 d->p: days since 0001/01/01
14:57:05 ==> 53,825 d->t: secs since midnight
A4 B4 ==> A4000000 ignore all after invalid char
-1234 ==> 1,234- allows leading sign
1234- ==> 1,234- also allows trailing sign
123,456,789 ==> 123456789 rightmost byte reserved for sign
123,456,789- ==> 123456789- only negative numbers use it, but
1,234,567,899 ==> 1234567899 +ve nums that need it use it too
12,345,678,901 ==> *345678901 overflow indicated by leading *
12,345 ==> 12345 leading zeros are suppressed
0 ==> 0 zero in = zero out
==> 0 blanks in = zero out
</PRE>
</BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -