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