ABAP 的一些特殊语法汇总

2016-08-30 浏览

[MOVE]  

DATA: F1(8) VALUE 'ABCDEFGH'.

MOVE F1+2(4) to F2.      "偏移量赋值.

WRITE  F2.    "输出结果: CDEF 

*------------------------------------------------------------------------------------*

DATA: BEGIN OF ADDRESS,
         FIRSTNAME(20) VALUE 'Fred',
         SURNAME(20) VALUE 'Flintstone',
         INITIALS(4) VALUE 'FF',
         STREET(20) VALUE 'Cave Avenue,
         NUMBER TYPE I VALUE '11'.
         POSTCODE TYPE N VALUE '98765'.

CITY(20) VALUE  'Bedrock',
      END OF ADDRESS.

DATA: BEGIN OF NAME,
         SURNAME(20),
         FIRSTNAME(20),
         INITIALS(4),
         TITLE(10) VALUE 'Mister',
      END OF NAME.

                MOVE-CORRESPONDING ADDRESS TO NAME      对应的数据结构赋值

此外对应的语法还有:

   ADD-CORRESPONDING

   SUBTRACT-CORRESPONDING

   MULTIPLY-CORRESPONDING

   DIVIDE-CORRESPONDING

*------------------------------------------------------------------------------------*   

 

处理日期和时间字段

 

处理日期和时间字段时,使用偏移量指定常常十分有用

DATA: ODATE TYPE D VALUE '19955011',
      IDATE LIKE ODATE.

DATA  FIELD(8).

FIELD = ODATE.   WRITE / FIELD.

CONVERT DATE ODATE INTO INVERTED-DATE IDATE.

FIELD = IDATE.   WRITE / FIELD.

CONVERT INVERTED-DATE IDATE INTO DATE ODATE.

FIELD = ODATE.   WRITE / FIELD.

输出如下:

19955011

80049488

19955011

 

 

   处理字符串


本字符串内部移动

语法

SHIFT <c> [BY <n> PLACES] [<mode>].

该语句将字  <c> 移动 <n> 个位置。如 果省略 BY <n> PLACES 则将 <n> 解释为一个 位置。如果 <n>  0 或负值,则 <c>保持不变。 如果 <n> 超过 <c> 长度,则 <c> 用空格填充 <n> 可为变量。

对不同(<mode> 选项,可以 按以下方式 移动字段 <c>

   LEFT:

向左移动 <n> 位置,右边  <n> 个空格填充 (默认设置 )。

   RIGHT:

向右移动 <n> 位置,左边  <n> 个空格填充 

   CIRCULAR:

向左移动 <n> 位置,以便 左边 <n> 个字符出现 在右边。

 

DATA: T(10) VALUE 'abcdefghij',
      STRING LIKE T.

STRING = T.
WRITE STRING.

SHIFT STRING.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES LEFT.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES RIGHT.
WRITE / STRING.

STRING = T.
SHIFT STRING BY 3 PLACES CIRCULAR.
WRITE / STRING.

输出为:

abcdefghij

bcdefghij

defghij

   abcdefg

defghijabc

其它字符串的赋值或移动,都可以用move 偏移量来实现,此不再述.

 

搜索字符串
语法

SEARCH <c> FOR <str> <options>.

该语句在字  <c> 中搜索<str> 中的字符串 。如果成功 则将 SY-SUBRC 的返回代码 值设置为0  SY-FDPOS 设置为字段<c> 中该字符串 的偏移量。 否则将 SY-SUBRC 设置为4

<str>

 

<pattern>

  <pattern> 任何字符顺 序)。忽略尾部空格。

 .<pattern>.

  <pattern> ,但是不忽略尾部空格 

*<pattern>

 索以 <pattern> 结尾的词。

 <pattern>*

 索以 <pattern> 开始的词。

 DATA STRING(30) VALUE 'This is a little sentence.'.

WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.      字符位置
ULINE /1(26).

SEARCH STRING FOR 'X'.
WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC',
               SY-FDPOS UNDER 'SY-FDPOS'

SEARCH STRING FOR 'itt   '.
WRITE: / 'itt   ', SY-SUBRC UNDER 'SY-SUBRC',
                   SY-FDPOS UNDER 'SY-FDPOS'

SEARCH STRING FOR '.e .'.
WRITE: / '.e .', SY-SUBRC UNDER 'SY-SUBRC',
                  SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR '*e'.
WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC',
                SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR 's*'.
WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',
                SY-FDPOS UNDER 'SY-FDPOS'.

该过程的输 出如下:

SEARCHED SY-SUBRC SY-FDPOS

X            4        0

itt          0       11

.e .         0       15

*e           0       10

s*           0       17

免费注册