You can address subfields in any statement in which non-numeric elementary ABAP types (c, d, n, t, x) or structures that do not contain internal tables occur using the following syntax:
f[+off][(len)]
By specifying an offset +off and a length (len) directly after the field name f, you can address the part of the field starting at position off+1 with length len as though it were an independent data object. In all statements, offset off and length len can be statically specified as number literals without a plus/minus sign or they can be specified dynamically using variables.
The data type and length of the string section are as follows:
Original field |
Subfield |
|
Data type |
Data type |
Length |
c |
c |
len |
d |
n |
len |
n |
n |
len |
t |
n |
len |
x |
x |
len |
Structure |
c |
len |
If you do not specify the length len, you address the section of the subfield between off and the end of the field. If the offset and length combination that you specify leads to an invalid section of the field (for example, exceeding the length of the original field), a syntax or runtime error occurs. You cannot use offset and length to address a literal or a text symbol.
You should take particular care when addressing components of structures. To ensure the correct platform-specific alignment of type i and f components, they may contain filler fields, whose lengths you need to consider when calculating the correct offset. Furthermore, conversion of the internal representation of character types to UNICODE is planned. One character will no longer occupy one byte, but two or four instead. Although offset and length specifications can work for character fields (types c, d, n, t) or for hexadecimal fields (type x), incompatible changes may occur in structures containing a mixture of numeric, character, and hexadecimal fields. SAP therefore recommends that you do not use offset and length to address components of structures.
DATA time TYPE t VALUE '172545'.
WRITE
time.
WRITE / time+2(2).
CLEAR time+2(4).
WRITE / time.
The output appears as follows:
172545
25
170000
First, the minutes are selected by specifying an offset in the WRITE statement. Then, the minutes and seconds are set to their initial values by specifying an offset in the CLEAR statement.
For the MOVE statement, the syntax for specifying offset and length is as follows:
MOVE f1[+off1][(len1)] TO f2[+off2][(len2)].
Or, when you use the assignment operator:
f2[+off2][(len2)] = f1[+off1][(len1)].
The contents of the part of the field f1 that begins at position off1+1 and has a length of len1 are assigned to field f2, where they overwrite the section that begins at position off2+1 and has a length of len2.
SAP recommends that you assign values with offset and length specifications only between non-numeric fields. With numeric fields, the results can be meaningless.
DATA:
f1(8) TYPE c VALUE 'ABCDEFGH',
f2(20) TYPE c VALUE
'12345678901234567890'.
f2+6(5) = f1+3(5).
In this example, the assignment operator functions as follows:
DATA:
f1(8) TYPE c VALUE 'ABCDEFGH',
f2(8).
TYPE c.
DATA: o
TYPE i VALUE 2,
l TYPE i VALUE 4.
MOVE
f1 TO
f2. WRITE
f2.
MOVE f1+o(l) TO
f2. WRITE / f2.
MOVE f1 TO
f2+o(l). WRITE /
f2.
CLEAR
f2.
MOVE f1 TO
f2+o(l). WRITE / f2.
MOVE f1+o(l) TO f2+o(l). WRITE / f2.
This produces the following output:
ABCDEFGH
CDEF
CDABCD
ABCD
CDEF
First, the contents of f1 are assigned to f2 without offset specifications. Then, the same happens for f1 with offset and length specification. The next three MOVE statements overwrite the contents of f2 with offset 2. Note that f2 is filled with spaces on the right, in accordance with the conversion rule for source type c.
For the WRITE TO statement, the syntax for specifying offset and length is as follows:
WRITE f1[+off1][(len1)] TO f2[+off2][(len2)].
The contents of the subfield o f field f1 that begins at position off1+1 and has a length of len1 are assigned to field f2, where they overwrite the section that begins at position off2+1 and has a length of len2.
DATA:
string(20) TYPE c,
number(8) TYPE c VALUE '123456',
offset TYPE i VALUE 8,
length TYPE i VALUE 12.
WRITE
number(6) TO string+offset(length) LEFT-JUSTIFIED.
WRITE: / string.
CLEAR string.
WRITE
number(6) TO string+offset(length) CENTERED.
WRITE: / string.
CLEAR string.
WRITE
number TO string+offset(length) RIGHT-JUSTIFIED.
WRITE: / string.
CLEAR string.
This produces the following output:
123456
123456
123456
The first six characters of the field number are written left-justified, centered, and right-justified into the last 12 characters of the field string.