ABAP--通过Field-symbols修改内表(ABAP新手)

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>.

参考链接:http://blog.csdn.net/CompassButton/archive/2007/06/15/1653907.aspx
http://blog.csdn.net/CompassButton/archive/2007/06/15/1653157.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值