ITAB所有字段值累加模版

REPORT z_jason_test_addfield .

DATA: BEGIN OF itab OCCURS 0,
      f1 TYPE i,
      f2 TYPE i,
      f3 TYPE i,
      f4 TYPE i,
      END OF itab.
DATA: total TYPE i.
PERFORM. writeitab.
PERFORM. addfield TABLES itab
                 CHANGING total.
WRITE:/ total.
*---------------------------------------------------------------------*
*       FORM. writeitab                                                *
*---------------------------------------------------------------------*
FORM. writeitab.
  itab-f1 = 1.
  itab-f2 = 2.
  itab-f3 = 3.
  itab-f4 = 4.
  APPEND itab.
  itab-f1 = 2.
  itab-f2 = 3.
  itab-f3 = 4.
  itab-f4 = 5.
  APPEND itab.
  itab-f1 = 2.
  itab-f2 = 3.
  itab-f3 = 4.
  itab-f4 = 5.
  APPEND itab.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  addfield
*&---------------------------------------------------------------------*
FORM. addfield TABLES itab
              CHANGING tmp TYPE i.
  DATA: BEGIN OF headtab OCCURS 0 ,
          length    TYPE i ,
          decimals  TYPE i,
          type_kind TYPE c,
          name(30)  TYPE c,
        END OF headtab.
  DATA: sum TYPE i,
        n TYPE i,
        count TYPE i ,
        count2 LIKE count,
        descr_ref TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS: TYPE abap_compdescr .
  FIELD-SYMBOLS TYPE ANY.
  descr_ref ?= cl_abap_typedescr=>describe_by_data( itab ).
  LOOP AT descr_ref->components ASSIGNING .
    MOVE-CORRESPONDING TO headtab.
    APPEND headtab.
  ENDLOOP.
  DESCRIBE TABLE headtab LINES n.
  LOOP AT itab.
    count = n.
    DO count TIMES.
      ASSIGN COMPONENT count OF STRUCTURE itab TO .
      sum = sum + .
      count = count - 1.
    ENDDO.
    total = total + sum.
    CLEAR sum.
  ENDLOOP.
ENDFORM.                    " addfield

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7762936/viewspace-422992/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7762936/viewspace-422992/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值