技术开发 频道

ABAP实例:内表重复字段如何删除

【IT168 技术文章】

* 処理概要:
*    内表重复字段如何删除

*   开始的数据如下:

  f1            f2               f3                              f4                   f5

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

 a              100          2009-01-01                   20                 2009-01-01

 a              100          2009-01-01                   30                 2009-01-03

a              100          2009-01-01                    50                 2009-01-04

 a             30            2009-01-12                    30                 2009-01-14

 b             20            2009-01-03                    10                 2009-01-22

最后得到的结果数格式为:

f1            f2               f3                              f4                   f5

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

 a              100          2009-01-01                   20                 2009-01-01

                                    30                 2009-01-03

                                      50                 2009-01-04

             30            2009-01-12                    30                 2009-01-14

 b             20            2009-01-03                    10                 2009-01-22

*----------------------------------------------------------------------*
* 使用テーブル:
*    IT_TABLE
*   
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
*                                                           方法一
*----------------------------------------------------------------------*

REPORT  Z_TEST5 .

*----------------------------------------------------------------------*
* 类型定义
*----------------------------------------------------------------------*
TYPES:
  BEGIN OF T_STR_CLASS,
    f1(1)    TYPE C,
    f2(5)    TYPE C,
    f3(12)   TYPE C,
    f4(10)   TYPE C,
    f5(12)   TYPE C,
END OF T_STR_CLASS,

T_IT_TABLE   TYPE STANDARD TABLE OF T_STR_CLASS.

*----------------------------------------------------------------------*
* 变量定义
*----------------------------------------------------------------------*

DATA:
  STR_CLASS   TYPE  T_STR_CLASS,
  IT_TABLE    TYPE  T_IT_TABLE.

*----------------------------------------------------------------------*
* 主处理
*----------------------------------------------------------------------*

"内表中写入数据

STR_CLASS-f1 = 'a'.
STR_CLASS-f2 = '100'.
STR_CLASS-f3 = '2009-1-1'.
STR_CLASS-f4 = '20'.
STR_CLASS-f5 = '2009-1-1'.
APPEND STR_CLASS TO IT_TABLE.
CLEAR STR_CLASS.

"内表中写入数据

STR_CLASS-f1 = 'a'.
STR_CLASS-f2 = '100'.
STR_CLASS-f3 = '2009-1-1'.
STR_CLASS-f4 = '30'.
STR_CLASS-f5 = '2009-1-3'.
APPEND STR_CLASS TO IT_TABLE.
CLEAR STR_CLASS.

"内表中写入数据

STR_CLASS-f1 = 'a'.
STR_CLASS-f2 = '100'.
STR_CLASS-f3 = '2009-1-1'.
STR_CLASS-f4 = '50'.
STR_CLASS-f5 = '2009-1-4'.
APPEND STR_CLASS TO IT_TABLE.
CLEAR STR_CLASS.

STR_CLASS-f1 = 'a'.
STR_CLASS-f2 = '30'.
STR_CLASS-f3 = '2009-1-12'.
STR_CLASS-f4 = '30'.
STR_CLASS-f5 = '2009-1-14'.
APPEND STR_CLASS TO IT_TABLE.
CLEAR STR_CLASS.

"内表中写入数据

STR_CLASS-f1 = 'b'.
STR_CLASS-f2 = '20'.
STR_CLASS-f3 = '2009-1-3'.
STR_CLASS-f4 = '10'.
STR_CLASS-f5 = '2009-1-22'.
APPEND STR_CLASS TO IT_TABLE.
CLEAR STR_CLASS.

"对内表按照f1,f2,f3这三个字段进行排序

SORT IT_TABLE BY f1 f2 f3.

"输出列头
WRITE: /1 'F1', 5 'F2', 12 'F3', 26 'F4', 38 'F5'.

"遍历内表数据显示
LOOP AT IT_TABLE INTO STR_CLASS.

"对于f1列内容改变时,显示到屏幕,不改变不显示(*AT NEW的用法见最后)

 AT NEW f1.
   WRITE: /1 STR_CLASS-f1.
 ENDAT.

"对于f2列内容改变时,显示到屏幕,不改变不显示

 AT NEW f2.
   WRITE: 5 STR_CLASS-f2.
 ENDAT.

"对于f3列内容改变时,显示到屏幕,不改变不显示

 AT NEW f3.
   WRITE: 12 STR_CLASS-f3.
 ENDAT.

"对于f4,f5列内容显示到屏幕

 WRITE: 26 STR_CLASS-f4, 38 STR_CLASS-f5.

"换新行
 NEW-LINE.

ENDLOOP. 

*----------------------------------------------------------------------*
*                                                           方法二
*----------------------------------------------------------------------*

  Z_TEST5 .

*----------------------------------------------------------------------*
* 变量定义
*----------------------------------------------------------------------*

"定义一个具有列结构的内表

DATA: BEGIN OF IT_TABLE OCCURS 10,
      f1(1)     TYPE  C,
      f2(5)     TYPE  C,
      f3(12)    TYPE  C,
      f4(10)    TYPE  C,
      f5(12)    TYPE  C,
    END OF IT_TABLE.

"定义一个和该内表含有相同结构的内表

DATA IT_TEST LIKE IT_TABLE.

"定义一个和该内表含有相同结构的字段符号

FIELD-SYMBOLS <FS> LIKE IT_TABLE.

"向内表中放入数据

IT_TABLE-f1 = 'a'.
IT_TABLE-f2 = '100'.
IT_TABLE-f3 = '2009-1-1'.
IT_TABLE-f4 = '20'.
IT_TABLE-f5 = '2009-1-1'.
APPEND IT_TABLE.

"向内表中放入数据

IT_TABLE-f1 = 'a'.
IT_TABLE-f2 = '100'.
IT_TABLE-f3 = '2009-1-1'.
IT_TABLE-f4 = '30'.
IT_TABLE-f5 = '2009-1-3'.
APPEND IT_TABLE.

"向内表中放入数据

IT_TABLE-f1 = 'a'.
IT_TABLE-f2 = '100'.
IT_TABLE-f3 = '2009-1-1'.
IT_TABLE-f4 = '50'.
IT_TABLE-f5 = '2009-1-4'.
APPEND IT_TABLE.

"向内表中放入数据

IT_TABLE-f1 = 'a'.
IT_TABLE-f2 = '30'.
IT_TABLE-f3 = '2009-1-12'.
IT_TABLE-f4 = '30'.
IT_TABLE-f5 = '2009-1-14'.
APPEND IT_TABLE.

"向内表中放入数据

IT_TABLE-f1 = 'b'.
IT_TABLE-f2 = '20'.
IT_TABLE-f3 = '2009-1-3'.
IT_TABLE-f4 = '10'.
IT_TABLE-f5 = '2009-1-22'.
APPEND IT_TABLE.

"对内表按照f1,f2,f3三列进行排序

SORT IT_TABLE BY f1 f2 f3.

"把内表列付给字段符号,这样字段符号,就指向了内表的那一列的值

LOOP AT IT_TABLE ASSIGNING <FS> .

"这句话的意思我不是很懂,大家有清楚的告诉我下。是一个值还是一列还是整个表?
    IT_TABLE = <FS>.

"比较IT_TEST内表中的f1字段的值与<fs>指向的f1的值是不是相同
 IF IT_TEST-F1 = <FS>-F1 .

"相同的话清空
    CLEAR <FS>-F1.
    IF IT_TEST-F2 = <FS>-F2.
    CLEAR <FS>-F2.
       IF IT_TEST-F3 = <FS>-F3.
         CLEAR <FS>-F3.
       ENDIF.
    ENDIF.


 ENDIF.
   IT_TEST = IT_TABLE.
ENDLOOP.

"显示到屏幕

LOOP AT IT_TABLE.
  WRITE : / IT_TABLE-F1,IT_TABLE-F2,IT_TABLE-F3,IT_TABLE-F4.
ENDLOOP.

0
相关文章