欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

abap 动态指针理解加深ASSIGN COMPONENT

"ASSIGN COMPONENT 运用

REPORT AA.
TABLES:ZTFI029.
TYPES:BEGIN OF ZTFI029,
        MANDT   TYPE STRING,
        KUNRG   TYPE STRING,
        MATNR   TYPE STRING,
        CHARG   TYPE STRING,
        Z_CPMC  TYPE STRING,
        Z_GGXH  TYPE STRING,
        Z_MEINS TYPE STRING,
      END OF ZTFI029.
TYPES:BEGIN OF TY_DATA,
        ROW  TYPE I,
        COL  TYPE I,
        TEST TYPE STRING,
        NAME TYPE STRING,
      END OF TY_DATA.
DATA:WA_DATA TYPE TY_DATA.
DATA:LT_DATA TYPE STANDARD TABLE OF TY_DATA.
DATA:LT_ZTFI029 TYPE STANDARD TABLE OF ZTFI029 WITH HEADER LINE.
DATA:LT_ZTFI029_01 TYPE STANDARD TABLE OF ZTFI029 WITH HEADER LINE.
DATA:NUM TYPE I.
DATA:NUM_01 TYPE STRING.
FIELD-SYMBOLS:<FS>.
LT_DATA = VALUE #( ( ROW = 1 COL = 1 TEST = 'KUNRG' NAME = 'AA' )
( ROW = 1 COL = 2 TEST = 'CHARG' NAME = 'BB' )
( ROW = 1 COL = 3 TEST = 'Z_CPMC' NAME = 'CC' )
( ROW = 1 COL = 4 TEST = 'Z_MEINS' NAME = 'DD' ) ).

LOOP AT LT_DATA INTO WA_DATA.
  NUM = WA_DATA-COL.
  ASSIGN COMPONENT NUM OF STRUCTURE LT_ZTFI029 TO <FS>. "如果没有指定固定的字段,则按照顺序进行指针的指向
  <FS> = WA_DATA-NAME.
  AT END OF ROW.
    APPEND LT_ZTFI029.
    CLEAR LT_ZTFI029.
  ENDAT.
  CLEAR:WA_DATA.
ENDLOOP.

LOOP AT LT_ZTFI029.
  WRITE:/ 'MANDT为',LT_ZTFI029-MANDT,
  '|KUNRG为',LT_ZTFI029-KUNRG,
  '|MATNR为',LT_ZTFI029-MATNR,
  '|CHARG为',LT_ZTFI029-CHARG,
  '|Z_CPMC为',LT_ZTFI029-Z_CPMC,
  '|Z_GGXH为',LT_ZTFI029-Z_GGXH,
  '|Z_MEINS为',LT_ZTFI029-Z_MEINS.
ENDLOOP.

WRITE:/ SY-ULINE.

LOOP AT LT_DATA INTO WA_DATA.
  NUM_01 = WA_DATA-TEST.
  "如果指定了字段的名称,则按照字段名称进行指针的指向,
  "理解为LT_ZTFI029_01的NUM_01字段指向了<FS>,下面<FS>赋值,即等于LT_ZTFI029_01的NUM_01字段赋值
  ASSIGN COMPONENT NUM_01 OF STRUCTURE LT_ZTFI029_01 TO <FS>.
  <FS> = WA_DATA-NAME.
  AT END OF ROW.
    APPEND LT_ZTFI029_01.
    CLEAR LT_ZTFI029_01.
  ENDAT.
  CLEAR:WA_DATA.
ENDLOOP.
LOOP AT LT_ZTFI029_01.
  WRITE:/ 'MANDT为',LT_ZTFI029_01-MANDT,
  '|KUNRG为',LT_ZTFI029_01-KUNRG,
  '|MATNR为',LT_ZTFI029_01-MATNR,
  '|CHARG为',LT_ZTFI029_01-CHARG,
  '|Z_CPMC为',LT_ZTFI029_01-Z_CPMC,
  '|Z_GGXH为',LT_ZTFI029_01-Z_GGXH,
  '|Z_MEINS为',LT_ZTFI029_01-Z_MEINS.
ENDLOOP.
WRITE:/ SY-ULINE.

 

posted @ 2020-04-24 15:59  萧静默  阅读(4910)  评论(0编辑  收藏  举报