二维码

[基础语法] ABAP动态指针Field symbol使用案例

Twilight发表于 2015-08-19 17:33Twilight 最后回复于 2015-08-19 17:33 [复制链接] 15145 0

通过一些小案例来熟悉动态指针Field symbol的使用。

1、Full type specification
  1. DATA: BEGIN OF line,
  2.         col1(1) TYPE c,
  3.         col2(1) TYPE c VALUE 'X',
  4.       END OF line.
  5. FIELD-SYMBOLS <fs> LIKE line.

  6. ASSIGN line TO <fs>.
  7. <fs>-col1 = <fs>-col2.
  8. WRITE: <fs>-col1, <fs>-col2.
复制代码
输出结果:X,X

2、Forcing structures
  1. DATA: wa(10) TYPE c VALUE '0123456789'.
  2. DATA: BEGIN OF line1,
  3.         col1(3) TYPE c,
  4.         col2(2) TYPE c,
  5.         col3(5) TYPE c,
  6.       END OF line1.
  7. DATA: BEGIN OF line2,
  8.         col1(2) TYPE c,
  9.         col2    TYPE sy-datum,
  10.       END OF line2.

  11. * obsolete -------------------------------------------------------------
  12. FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,
  13.                <f2> STRUCTURE line2 DEFAULT wa.

  14. * correct --------------------------------------------------------------
  15. FIELD-SYMBOLS <f3> LIKE line1.
  16. ASSIGN wa TO <f3> CASTING.

  17. FIELD-SYMBOLS <f4> LIKE line2.
  18. ASSIGN wa TO <f4> CASTING.

  19. * ----------------------------------------------------------------------
  20. WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,
  21.        / <f2>-col1, <f2>-col2.
  22. SKIP.
  23. WRITE: / <f3>-col1, <f3>-col2, <f3>-col3,
  24.        / <f4>-col1, <f4>-col2.
复制代码
输出结果:
  1. 012 34 56789
  2. 01 2345.67.89

  3. 012 34 56789
  4. 01 2345.67.89
复制代码

3、Static assign
  1. FIELD-SYMBOLS: <f1> TYPE any, <f2> TYPE i.
  2. DATA: text(20) TYPE c VALUE 'Hello, how are you?',
  3.       num      TYPE i VALUE 5,
  4.       BEGIN OF line1,
  5.         col1 TYPE f VALUE '1.1e+10',
  6.         col2 TYPE i VALUE '1234',
  7.       END OF line1,
  8.       line2 LIKE line1.

  9. ASSIGN text TO <f1>.
  10. ASSIGN num TO  <f2>.
  11. DESCRIBE FIELD <f1> LENGTH <f2> IN BYTE MODE.
  12. WRITE: / <f1>, 'has length', num.

  13. ASSIGN line1 TO <f1>.
  14. ASSIGN line2-col2 TO <f2>.
  15. MOVE <f1> TO line2.
  16. ASSIGN 'LINE2-COL2 =' TO <f1>.
  17. WRITE: / <f1>, <f2>.
复制代码
输出结果:
  1. Hello, how are you?  has length         40
  2. LINE2-COL2 =      1,234
复制代码

4、Assign with offset 偏移量n(m),表示截取从n+1(包括n+1)开始长度为m的字符串
  1. FIELD-SYMBOLS <fs> TYPE any.
  2. DATA: BEGIN OF line,
  3.         string1(10) VALUE '0123456789',
  4.         string2(10) VALUE 'abcdefghij',
  5.       END OF line.

  6. WRITE / line-string1+5.

  7. ASSIGN line-string1+5(*) TO <fs>.
  8. WRITE / <fs>.
复制代码
输出结果:
  1. 56789
  2. 56789
复制代码

6、Dynamic assign
  1. TABLES sbook.
  2. DATA: name1(20) TYPE c VALUE 'SBOOK-FLDATE',
  3.       name2(20) TYPE c VALUE 'NAME1'.
  4. FIELD-SYMBOLS <fs> TYPE any.

  5. ASSIGN TABLE FIELD (name1) TO <fs>.
  6. WRITE: / 'SY-SUBRC:', sy-subrc.

  7. ASSIGN TABLE FIELD (name2) TO <fs>.
  8. WRITE: / 'SY-SUBRC:', sy-subrc.
复制代码
输出结果:
  1. SY-SUBRC:     0
  2. SY-SUBRC:     4
复制代码
(name1) 是call by value 的用法,取的是name1里的值SBOOK-FLDATE
7、Assigning a structure by component
  1. DATA: BEGIN OF line,
  2.         col1 TYPE i VALUE '11',
  3.         col2 TYPE i VALUE '22',
  4.         col3 TYPE i VALUE '33',
  5.       END OF line.
  6. DATA comp(5) TYPE c VALUE 'COL3'.
  7. FIELD-SYMBOLS: <f1> TYPE any, <f2> TYPE any, <f3> TYPE any.

  8. ASSIGN line TO <f1>.
  9. ASSIGN comp TO <f2>.

  10. DO 3 TIMES.
  11.   ASSIGN COMPONENT sy-index OF STRUCTURE <f1> TO <f3>.
  12.   WRITE <f3>.
  13. ENDDO.

  14. ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.
  15. WRITE / <f3>.
复制代码
输出结果:
  1.         11          22          33
  2.         33
复制代码

8、Casting with field symbol type:CASTING主要用来指定<fs>的隐式数据类型t_date
  1. TYPES: BEGIN OF t_date,
  2.          year(4)  TYPE n,
  3.          month(2) TYPE n,
  4.          day(2)   TYPE n,
  5.        END OF t_date.
  6. FIELD-SYMBOLS <fs> TYPE t_date.

  7. ASSIGN sy-datum TO <fs> CASTING.

  8. WRITE / sy-datum.
  9. SKIP.
  10. WRITE: / <fs>-year , / <fs>-month, / <fs>-day.
复制代码
输出结果:
  1. 2015.08.19

  2. 2015
  3. 08
  4. 19
复制代码

9、Casting with explicit type:CASTING主要用来指定<fs>的显式数据类型t_date
  1. TYPES: BEGIN OF t_date,
  2.          year(4)  TYPE n,
  3.          month(2) TYPE n,
  4.          day(2)   TYPE n,
  5.        END OF t_date.
  6. FIELD-SYMBOLS: <fs> TYPE any,
  7.                <f>  TYPE n.

  8. ASSIGN sy-datum TO <fs> CASTING TYPE t_date.
  9. WRITE / sy-datum.
  10. SKIP.
  11. DO.
  12.   ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.
  13.   IF sy-subrc <> 0.
  14.     EXIT.
  15.   ENDIF.
  16.   WRITE / <f>.
  17. ENDDO.
复制代码
结果同上
10、Casting with predefined data types
  1. DATA txt(8) TYPE c VALUE '19980606'.
  2. DATA mytype(1) VALUE 'X'.
  3. FIELD-SYMBOLS <fs> TYPE any.

  4. ASSIGN txt TO <fs>.
  5. WRITE / <fs>.

  6. SKIP.
  7. * obsolete -------------------------------------------------------------
  8. ASSIGN txt TO <fs> TYPE 'D'.
  9. WRITE / <fs>.

  10. ASSIGN txt TO <fs> TYPE mytype.
  11. WRITE / <fs>.

  12. SKIP.
  13. * correct --------------------------------------------------------------
  14. ASSIGN txt TO <fs> CASTING TYPE d.
  15. WRITE / <fs>.

  16. ASSIGN txt TO <fs> CASTING TYPE (mytype).
  17. WRITE / <fs>.
复制代码
输出结果:
  1. 19980606

  2. 19980606
  3. 31003900390038003000360030003600

  4. 19980606
  5. 31003900390038003000360030003600
复制代码

11、Casting decimla places
  1. DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
  2.       pack2 TYPE p DECIMALS 2,
  3.       pack3 TYPE p DECIMALS 2.
  4. FIELD-SYMBOLS: <f1> TYPE any,
  5.                <f2> TYPE any.

  6. WRITE: / 'PACK1', pack1.
  7. SKIP.

  8. * obsolete -------------------------------------------------------------
  9. ASSIGN pack1 TO <f1> DECIMALS 1.
  10. WRITE: / '<F1> ', <f1>.

  11. pack2 = <f1>.
  12. WRITE: / 'PACK2', pack2.

  13. ASSIGN pack2 TO <f2> DECIMALS 4.
  14. WRITE: / '<F2> ', <f2>.

  15. pack3 = <f1> + <f2>.
  16. WRITE: / 'PACK3', pack3.

  17. <f2> = '1234.56789'.
  18. WRITE: / '<F2> ', <f2>.
  19. WRITE: / 'PACK2', pack2.

  20. SKIP.
  21. * correct --------------------------------------------------------------
  22. ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
  23. WRITE: / '<F1> ', <f1>.

  24. pack2 = <f1>.
  25. WRITE: / 'PACK2', pack2.

  26. ASSIGN pack2 TO <f2> CASTING TYPE p DECIMALS 4.
  27. WRITE: / '<F2> ', <f2>.

  28. pack3 = <f1> + <f2>.
  29. WRITE: / 'PACK3', pack3.

  30. <f2> = '1234.56789'.
  31. WRITE: / '<F2> ', <f2>.
  32. WRITE: / 'PACK2', pack2.
复制代码
输出结果:
  1. PACK1           400.00

  2. <F1>           4,000.0
  3. PACK2         4,000.00
  4. <F2>           40.0000
  5. PACK3         4,040.00
  6. <F2>        1,234.5679
  7. PACK2       123,456.79

  8. <F1>           4,000.0
  9. PACK2         4,000.00
  10. <F2>           40.0000
  11. PACK3         4,040.00
  12. <F2>        1,234.5679
  13. PACK2       123,456.79
复制代码

12、Data references:分配类的方法给指针
  1. TYPES: BEGIN OF t_struct,
  2.          col1 TYPE i,
  3.          col2 TYPE i,
  4.        END OF t_struct.
  5. DATA: dref1 TYPE REF TO data,
  6.       dref2 TYPE REF TO data.

  7. FIELD-SYMBOLS: <fs1> TYPE t_struct,
  8.                <fs2> TYPE i.

  9. CREATE DATA dref1 TYPE t_struct.

  10. ASSIGN dref1->* TO <fs1>.

  11. <fs1>-col1 = 1.
  12. <fs1>-col2 = 2.

  13. dref2 = dref1.

  14. ASSIGN dref2->* TO <fs2> CASTING.
  15. WRITE / <fs2>.

  16. GET REFERENCE OF <fs1>-col2 INTO dref2.

  17. ASSIGN dref2->* TO <fs2>.
  18. WRITE / <fs2>.
复制代码
输出结果:
  1.          1
  2.          2
复制代码

注意:ASSIGN TABLE FIELD (f) TO <fs>. 只能用TABLES定义的变量
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表