FIELD-SYMBOLS 实现动态字段的赋值操作

直接cv到自己程序就可运行.

这个案例用到了cl_abap_structdescr 我们通过这个类中的这个方法describe_by_data( ) 来获取结构中的相关字段,然后在通过components这个属性来获取具体的字段.

使用loop循环的方式分配给<fs_comp>.字段符号,这个字段符号的数据类型需要注意一下abap_compdescr,他这里面有一个属性name用来存放刚才分配过来的字段名,然后把它给到一张内表gt_field中就可以了.

gt_field这个内表中已经拿到结构中的所有字段名字了,接下来就可以动态性质的操作gt_field这个内表就行了.需要你给他拼接一下,也就是把原来的内表和拿到的字段拼接起来,再来操作原内表.(具体可看下面代码部分)

其实可以把它封装成一个函数模块,以后在用到时会很方便的.

如果你觉得这个比较有意思的话,可以去一趟彩票站,买个彩票.祝您中大奖!!

*&---------------------------------------------------------------------*
*& Report  ZTEST_JOB_02
*&---------------------------------------------------------------------*
report ztest_job_02.
types d_ty type c length 2.
types: begin of ty  ,
         a type d_ty,
         b type d_ty,
         c type d_ty,
         d type d_ty,
         e type d_ty,
         f type d_ty,
         h type d_ty,
         g type d_ty,
       end of  ty.

types:begin of typ_field,
        fieldnm type txt30,
      end of typ_field.

data :gt_  type table of ty,
      gs_  type ty.

data lv_index_1 type i value 1.
data lv_index_2 type i value 1.
data int1     type i value 1.
data lv_max   type i.

data:g_fieldnm type txt30.
data:cl_descr type ref to cl_abap_structdescr.
data:gt_field type table of typ_field with header line.

field-symbols:<fs_comp> type abap_compdescr.
field-symbols <fs_name> type any.

cl_descr ?= cl_abap_typedescr=>describe_by_data( gs_ ).

loop at cl_descr->components assigning <fs_comp>.
*  write: / <fs_comp>-name.             "字段名称
  append <fs_comp>-name to gt_field.
endloop.
loop at gt_field.
  concatenate 'GS_-' gt_field-fieldnm into gt_field.
  modify gt_field.
endloop.

*loop at gt_ into gs_ .
*  write /.
*  loop at gt_field.
*    assign (gt_field-fieldnm) to <fs_name>.
*    write: <fs_name>.
*  endloop.
*endloop.
delete gt_field where fieldnm = 'GS_-H'.
do 5 times.
  append gs_ to gt_.
  do 7 times.
    lv_max = cond #( when lv_index_1 = 7 then 16 else 33 ).
    call function 'QF05_RANDOM_INTEGER'
      exporting
        ran_int_max   = lv_max
        ran_int_min   = 1
      importing
        ran_int       = int1
      exceptions
        invalid_input = 1
        others        = 2.
   
   "下面这个三行就是动态操作的,现在只需要一行代码就可以对所有字段进行赋值操作了.
    read table gt_field into gt_field index lv_index_1.
    assign (gt_field-fieldnm) to <fs_name> .
    <fs_name> = int1.

    gs_-h = '|' .
    modify gt_ from gs_ index lv_index_2 .
    lv_index_1 = lv_index_1 + 1.
  enddo.
  lv_index_2 = lv_index_2 + 1.
  lv_index_1 = 1.
  clear gs_ .
enddo.
cl_demo_output=>display( gt_ ). "弹窗的形式展示
*DATA: lv_num1 TYPE i,
*        lv_num2 TYPE i,
*        lv_num3 TYPE i,
*        lv_num4 TYPE i,
*        lv_num5 TYPE i,
*        lv_num6 TYPE i.
*
*lv_num1  = abs( -1 ) . "获取绝对值
*lv_num2  = sign( 10 ) . 
*lv_num3  = ceil( 10 ) ."向上取整
*  WRITE:/ lv_num1, lv_num2, lv_num3, lv_num4, lv_num5, lv_num6.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨天行舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值