将半分离的字符串转换为表结构?
我需要一些帮助来将字符串转换为ITAB中。
LOOP AT LT_0111_FILE INTO LV_STRING.
SPLIT LV_STRING AT ';' INTO TABLE LT_0111.
DO GV_COMP TIMES.
READ TABLE LT_0111 ASSIGNING <LV> INDEX SY-INDEX.
IF <LV> IS NOT INITIAL.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <STRUCT> TO <LV_COMP>.
IF <LV_COMP> IS ASSIGNED.
<LV_COMP> = <LV>.
ENDIF.
ENDIF.
ENDDO.
INSERT <STRUCT> INTO TABLE <TABLE>.
ENDLOOP.
在lt_0111_file中是表PA0111作为带有分隔器的字符串“;”。 现在,我需要将此字符串的每个字段分配到PA0111的结构字段中。
我不想为每个字段分开执行此操作,因为这些字段将是动态创建的。
此代码适用于字符字段,但对数字不起作用。在TXT文件中,将有大约0,00的数字,将它们移至结构的字段,因为数字必须为0.00,因此会出现错误。
感谢您的帮助
I need some help for converting a string into in ITAB.
LOOP AT LT_0111_FILE INTO LV_STRING.
SPLIT LV_STRING AT ';' INTO TABLE LT_0111.
DO GV_COMP TIMES.
READ TABLE LT_0111 ASSIGNING <LV> INDEX SY-INDEX.
IF <LV> IS NOT INITIAL.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <STRUCT> TO <LV_COMP>.
IF <LV_COMP> IS ASSIGNED.
<LV_COMP> = <LV>.
ENDIF.
ENDIF.
ENDDO.
INSERT <STRUCT> INTO TABLE <TABLE>.
ENDLOOP.
In LT_0111_FILE is the table PA0111 as a string with separator ";".
Now I need to assign every field of this stringtable into the fields of the structure from PA0111.
I don´t want to do this for every field separatly because the fields will be created dynamically.
This code works for character fields but not for numbers. In the txt-file there will be numbers like 0,00 and to move them to the fields of the structure will give an error because the number have to be 0.00.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您有一个未知的结构并想知道这些字段的字段以及这些字段的属性时,您可以使用运行时类型信息类。
首先,获取目标结构的类型描述。
铸造 - 操作员
?=
在这里需要,因为descrip_by_data
的返回值是通用cl_abap_typedescr
。 您知道它必须是一个结构,但是班级不知道。它也可能是一个表格,简单类型或对对象的引用。但是,如果您可以保证它必须是一个结构,则可以将其铸造到cl_abap_StructDescr
。现在,您可以使用一个表来描述结构的所有字段:
此表包含这些组件的名称和类型。因此,您可以通过 使用使用使用使用
分配component
按名称,而不是使用do-loop和使用分配component
。然后,您可以根据其类型处理每个字段:When you have an unknown structure and want to know what fields it has and what properties those fields have, then you can use the runtime type information classes.
First, get a type description of your target structure.
The casting-operator
?=
is required here, because the return value ofdescribe_by_data
is a genericcl_abap_typedescr
. You know that it got to be a structure, but the class does not know that. It could just as well be a table, simple type or a reference to an object. But if you can guarantee that it got to be a structure, you can up-cast it to acl_abap_structdescr
.Now you can get a table describing all the fields of the structure with:
This table contains the names and the types of those components. So instead of using a DO-loop and using
ASSIGN COMPONENT
by index, you canLOOP AT
the component table and useASSIGN COMPONENT
by name. And then you can handle each field according to its type:实际上,我更喜欢飞利浦解决方案,在类型处理方面,它更具错误和全面,但仅出于多样性的目的,我将添加这个快速的dirty解决方案。
您可以使用
cl_rsda_csv_converter
辅助类的方法:Actually I like Philips solution more, it is more error-proof and comprehensive in terms of type-handling, but just for the sake of diversity I will add this fast&dirty solution.
You can use methods methods of
cl_rsda_csv_converter
auxiliary class: