• ABAPField Symbol 的Example(来自SAP的样例)


    Full type specification 

    REPORT demo_field_symbols_type .

    DATA: BEGIN OF line,
             col1(1) TYPE c,
             col2(1) TYPE c VALUE 'X',
           END OF line.

    FIELD-SYMBOLS <fs> LIKE line.

    ASSIGN line TO <fs>.

    MOVE <fs>-col2 TO <fs>-col1.

    WRITE: <fs>-col1, <fs>-col2.


    Forcing structures

     REPORT demo_field_symbols_structure .

    DATA: wa(10) TYPE c VALUE '0123456789'.

    DATA: BEGIN OF line1,
             col1(3) TYPE c,
             col2(2) TYPE c,
             col3(5) TYPE c,
          END OF line1.

    DATA: BEGIN OF line2,
             col1(2) TYPE c,
             col2 TYPE sy-datum,
          END OF line2.

    * obsolete -------------------------------------------------------------

    FIELD-SYMBOLS: <f1> STRUCTURE line1 DEFAULT wa,
                   <f2> STRUCTURE line2 DEFAULT wa.

    * correct --------------------------------------------------------------

    FIELD-SYMBOLS <f3> LIKE line1.
    ASSIGN wa TO <f3> CASTING.

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

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

    WRITE: / <f1>-col1, <f1>-col2, <f1>-col3,
           / <f2>-col1, <f2>-col2.

    SKIP.

    WRITE: / <f3>-col1, <f3>-col2, <f3>-col3,
           / <f4>-col1, <f4>-col2. 


    Static assign

     REPORT demo_field_symbols_stat_assign .

    FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE i.

    DATA: text(20)  TYPE c VALUE 'Hello, how are you?',
          num       TYPE i VALUE 5,
          BEGIN OF line1,
            col1 TYPE f VALUE '1.1e+10',
            col2 TYPE i VALUE '1234',
          END OF line1,
          line2 LIKE line1.

    ASSIGN text TO <f1>.
    ASSIGN num TO  <f2>.
    DESCRIBE FIELD <f1> LENGTH <f2>.
    WRITE: / <f1>, 'has length', num.

    ASSIGN line1 TO <f1>.
    ASSIGN line2-col2 TO <f2>.
    MOVE <f1> TO line2.
    ASSIGN 'LINE2-COL2 =' TO <f1>.
    WRITE: / <f1>, <f2>.


    Assign with offset

     REPORT demo_field_symbols_stat_as_off .

    FIELD-SYMBOLS <fs> TYPE ANY.

    DATA: BEGIN OF line,
            string1(10) VALUE '0123456789',
            string2(10) VALUE 'abcdefghij',
          END OF line.

    WRITE / line-string1+5.

    ASSIGN line-string1+5 TO <fs>.
    WRITE / <fs>.

    ASSIGN line-string1+5(*) TO <fs>.
    WRITE / <fs>.

    REPORT demo_field_symbols_stat_as_of2 .

    FIELD-SYMBOLS <fs> TYPE ANY.

    DATA: BEGIN OF line,
            a TYPE c VALUE '1', b TYPE c VALUE '2',
            c TYPE c VALUE '3', d TYPE c VALUE '4',
            e TYPE c VALUE '5', f TYPE c VALUE '6',
            g TYPE c VALUE '7', h TYPE c VALUE '8',
          END OF line,
          off TYPE i,
          len TYPE i VALUE 2.

    DO 2 TIMES.
      off = sy-index * 3.
      ASSIGN line-a+off(len) TO <fs>.
      <fs> = 'XX'.
    ENDDO.

    DO 8 TIMES.
      off = sy-index - 1.
      ASSIGN line-a+off(1) TO <fs>.
      WRITE <fs>.
    ENDDO.


    Dynamic assign

     REPORT demo_field_symbols_dynami_as_2 .

    TABLES sbook.

    DATA: name1(20) TYPE c VALUE 'SBOOK-FLDATE',
          name2(20) TYPE c VALUE 'NAME1'.

    FIELD-SYMBOLS <fs> TYPE ANY.

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

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


    Assigning field symbols

    REPORT demo_field_symbols_dynami_as_3 .

    DATA: BEGIN OF s,
            a TYPE c VALUE '1', b TYPE c VALUE '2', c TYPE c VALUE '3',
            d TYPE c VALUE '4', e TYPE c VALUE '5', f TYPE c VALUE '6',
            g TYPE c VALUE '7', h TYPE c VALUE '8',
          END OF s.

    DATA off TYPE i.

    FIELD-SYMBOLS <fs> TYPE ANY.

    ASSIGN s-a TO <fs>.

    DO 4 TIMES.
      off = sy-index - 1.
      ASSIGN <fs>+off(1) TO <fs>.
      WRITE <fs>.
    ENDDO. 


    Assigning a structure by component

    REPORT demo_field_symbols_assign_comp .

    DATA: BEGIN OF line,
            col1 TYPE i VALUE '11',
            col2 TYPE i VALUE '22',
            col3 TYPE i VALUE '33',
          END OF line.

    DATA comp(5) TYPE c VALUE 'COL3'.

    FIELD-SYMBOLS: <f1> TYPE ANY, <f2> TYPE ANY, <f3> TYPE ANY.

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

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

    ASSIGN COMPONENT <f2> OF STRUCTURE <f1> TO <f3>.
    WRITE / <f3>. 


    Casting with field symbol type

    REPORT demo_field_symbols_casting.

    TYPES: BEGIN OF t_date,
              year(4)  TYPE n,
              month(2) TYPE n,
              day(2)   TYPE n,
           END OF t_date.

    FIELD-SYMBOLS <fs> TYPE t_date.

    ASSIGN sy-datum TO <fs> CASTING.

    WRITE / sy-datum.
    SKIP.
    WRITE: / <fs>-year , / <fs>-month, / <fs>-day. 


    Casting with explicit type

    REPORT demo_field_symbols_casting_typ.

    TYPES: BEGIN OF t_date,
              year(4)  TYPE n,
              month(2) TYPE n,
              day(2)   TYPE n,
           END OF t_date.

    FIELD-SYMBOLS: <fs> TYPE ANY,
                   <f>  TYPE n.

    ASSIGN sy-datum TO <fs> CASTING TYPE t_date.

    WRITE / sy-datum.

    SKIP.

    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <f>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      WRITE / <f>.
    ENDDO. 


    Casting with predefined data types

    REPORT demo_field_symbols_assign_type .

    DATA txt(8) TYPE c VALUE '19980606'.

    DATA mytype(1) VALUE 'X'.

    FIELD-SYMBOLS <fs> TYPE ANY.

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

    SKIP.

    * obsolete -------------------------------------------------------------

    ASSIGN txt TO <fs> TYPE 'D'.
    WRITE / <fs>.

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

    SKIP.

    * correct --------------------------------------------------------------

    ASSIGN txt TO <fs> CASTING TYPE d.
    WRITE / <fs>.

    ASSIGN txt TO <fs> CASTING TYPE (mytype).
    WRITE / <fs>. 


    Casting decimla places

     REPORT demo_field_symbols_assign_deci .

    DATA: pack1 TYPE p DECIMALS 2 VALUE '400',
          pack2 TYPE p DECIMALS 2,
          pack3 TYPE p DECIMALS 2.

    FIELD-SYMBOLS: <f1> TYPE ANY ,
                   <f2> TYPE ANY.

    WRITE: / 'PACK1', pack1.

    SKIP.

    * obsolete -------------------------------------------------------------

    ASSIGN pack1 TO <f1> DECIMALS 1.
    WRITE: / '<F1> ', <f1>.

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

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

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

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

    SKIP.

    * correct --------------------------------------------------------------

    ASSIGN pack1 TO <f1> CASTING TYPE p DECIMALS 1.
    WRITE: / '<F1> ', <f1>.

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

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

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

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


    Data areas for field symbols

    REPORT demo_field_symbols_assign_err .

    DATA: text1(10) TYPE c, text2(10) TYPE c, text3(5) TYPE c.

    FIELD-SYMBOLS <fs> TYPE ANY.

    DO 100 TIMES.                          "Runtime-Error!
      ASSIGN text1+sy-index(1) TO <fs>.
    ENDDO. 


    Data references

    REPORT demo_data_reference.

    TYPES: BEGIN OF t_struct,
             col1 TYPE i,
             col2 TYPE i,
           END OF t_struct.

    DATA: dref1 TYPE REF TO data,
          dref2 TYPE REF TO data.

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

    CREATE DATA dref1 TYPE t_struct.

    ASSIGN dref1->* TO <fs1>.

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

    dref2 = dref1.

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

    GET REFERENCE OF <fs1>-col2 INTO dref2.

    ASSIGN dref2->* TO <fs2>.
    WRITE / <fs2>. 


    --------------------------------------------------------

    report demo_field_symbols_assign_comp .

    DATA: BEGIN OF gs_itab,
    drph(10) ,
    cmsl01(17),
    cmsl02(17),
    sl01(17),
    sl02(17),
    END OF gs_itab.
    DATA: gt_ita1 LIKE gs_itab OCCURS 0 WITH HEADER LINE.

    data gv_zd(15).

    FIELD-SYMBOLS <sl> TYPE c.
    FIELD-SYMBOLS <cmsl> TYPE c.
    FIELD-SYMBOLS <fs> LIKE LINE OF gt_ita1.

    DO 15 TIMES.
    gt_ita1-drph =  SY-INDEX .
    gt_ita1-cmsl01 = 2.
    append gt_ita1.
    enddo.

    write 'Before Modify:'.
    write:/ 'cmsl01','cmsl02','sl01','sl02'.
    loop at gt_ita1.
    write:/ '|',gt_ita1-cmsl01,'|',gt_ita1-cmsl02,'|',gt_ita1-sl01,'|' ,gt_ita1-sl02,'|'.
    endloop.
    loop at gt_ita1 aSSIGNING <fs>.
    ASSIGN COMPONENT 'CMSL01' OF STRUCTURE <fs> TO <cmsl>.
    ASSIGN COMPONENT 'SL01' OF STRUCTURE <fs> TO <sl>.
    move <cmsl>  to  <sl>.
    ASSIGN COMPONENT 'SL02' OF STRUCTURE <fs> TO <sl>.
    move <cmsl>  to  <sl>.
    <fs>-CMSL02 = 'A' .
    endloop.

    write / 'After Modify:'.
    loop at gt_ita1.
    write:/ '|',gt_ita1-cmsl01,'|',gt_ita1-cmsl02,'|',gt_ita1-sl01,'|' ,gt_ita1-sl02,'|'.
    endloop.

    注意:ASSIGN TABLE FIELD (f) TO <fs>. 只能用TABLES定义的变量
    An important, but frequently misunderstood aspect of ABAP, is the "Field Symbol". But you'll find they aren't mysterious. In fact, they may remind you of some features in popular general-purpose programming languages.
    Field symbols allow you to:
    ** Assign an alias to a data object(for example, a shortened name for data objects structured through several hierarchies
    For Example: <fs>-f instead of rec1-rec2-rec3-f)
    ** Set the offset and length for a string variably at runtime
    ** Set a pointer to a data object that you determine at runtime (dynamic ASSIGN)
    ** Adopt or change the type of a field dynamically at runtime
    ** Access components of a structure (from Release 4.5A) Point to lines of an internal table (process internal tables without a separate work area)

    Field symbols in ABAP are similar to pointers in other programming languages. However, pointers (as used in PASCAL or C) differ from ABAP field symbols in their reference syntax.

    The statement ASSIGN f to <fs> assigns the field f to field symbol <fs>. The field symbol <fs> then "points" to the contents of field f at runtime. This means that all changes to the
    contents of f are visible in <fs> and vice versa. You declare the field symbol <fs> using the statement FIELD-SYMBOLS: <fs>.

    Reference syntax
    Programming languages such as PASCAL and C use a dereferencing symbol to indicate the difference between a reference and the object to which it refers; so PASCAL would use p^ for a pointer instead of p, C would use *p instead of p. ABAP does not have any such dereferencing symbol.
    ** In PASCAL or C, if you assign a pointer p1 to a pointer p2, you force p1 to point to the object to which p2 refers (reference semantics).
    ** In ABAP, if you assign a field symbol <fs1> to a field  symbol <fs2>, <fs1> takes the value of the data object to which <fs2> refers (value semantics).
    ** Field symbols in ABAP are always dereferenced, that is, they always access the referenced data object. If you want to change the reference yourself in ABAP, you can use the ASSIGN statement
    to assign field symbol <fs1> to field symbol <fs2>.

  • 相关阅读:
    Ubuntu换源
    如何查看已购买的office密钥
    python的模块放在哪里
    centos7不能启动网卡报No suitable device found for this connection错误
    搭建邮件服务器 使用Postfix与Dovecot
    SSH远程免密码的密钥登录服务(Linux,Linux)
    SSH服务搭建、账号密码登录远程Linux虚拟机、基于密钥的安全验证(Windows_Xshell,Linux)
    apache基础,apache环境搭建,apache的3种使用方式(IP、端口、域名)
    Linux vsftpd服务配置以及三种验证方式以及常见错误解决办法
    使用DHCP动态管理主机地址
  • 原文地址:https://www.cnblogs.com/elegantok/p/1581582.html
Copyright © 2020-2023  润新知