将半分离的字符串转换为表结构?

发布于 2025-02-12 22:43:48 字数 732 浏览 2 评论 0原文

我需要一些帮助来将字符串转换为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2025-02-19 22:43:48

当您有一个未知的结构并想知道这些字段的字段以及这些字段的属性时,您可以使用运行时类型信息类。

首先,获取目标结构的类型描述。

DATA lo_struct_description TYPE REF TO cl_abap_structdescr.
lo_struct_description ?= cl_abap_typedescr=>describe_by_data( <struct> ).

铸造 - 操作员?=在这里需要,因为descrip_by_data的返回值是通用cl_abap_typedescr知道它必须是一个结构,但是班级不知道。它也可能是一个表格,简单类型或对对象的引用。但是,如果您可以保证它必须是一个结构,则可以将其铸造到cl_abap_StructDescr

现在,您可以使用一个表来描述结构的所有字段:

DATA lt_components TYPE abap_component_tab.
lt_components = lo_struct_description->get_components( ).

此表包含这些组件的名称和类型。因此,您可以通过 使用使用使用使用分配component按名称,而不是使用do-loop和使用分配component。然后,您可以根据其类型处理每个字段:

LOOP AT lt_components INTO ls_component.
  READ TABLE lt_0111 ASSIGNING <lv_in> INDEX sy-tabix.
  IF sy-subrc = 0.
    ASSIGN COMPONENT ls_component-name OF STRUCTURE <struct> TO <lv_out>.
    IF sy-subrc = 0.
      CASE ls_component-type->type_kind.
        WHEN cl_abap_datadescr=>typekind_date.
          " Special handling for dates
        WHEN cl_abap_datadescr=>typekind_packed.
          " Special handling for decimal numbers
         
        " Check the other constants cl_abap_datadescr=>typekind_* for all the other types
        " you might encounter in your use-case and which might require special treatment.
        WHEN OTHERS.
          " Probably just copyable. If not, you will get a runtime error here and need 
          " to implement special handling for this particular type_kind.
          <lv_out> = <lv_in>.
      ENDCASE.
    ENDIF.
  ENDIF.
ENDLOOP.

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.

DATA lo_struct_description TYPE REF TO cl_abap_structdescr.
lo_struct_description ?= cl_abap_typedescr=>describe_by_data( <struct> ).

The casting-operator ?= is required here, because the return value of describe_by_data is a generic cl_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 a cl_abap_structdescr.

Now you can get a table describing all the fields of the structure with:

DATA lt_components TYPE abap_component_tab.
lt_components = lo_struct_description->get_components( ).

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 can LOOP AT the component table and use ASSIGN COMPONENT by name. And then you can handle each field according to its type:

LOOP AT lt_components INTO ls_component.
  READ TABLE lt_0111 ASSIGNING <lv_in> INDEX sy-tabix.
  IF sy-subrc = 0.
    ASSIGN COMPONENT ls_component-name OF STRUCTURE <struct> TO <lv_out>.
    IF sy-subrc = 0.
      CASE ls_component-type->type_kind.
        WHEN cl_abap_datadescr=>typekind_date.
          " Special handling for dates
        WHEN cl_abap_datadescr=>typekind_packed.
          " Special handling for decimal numbers
         
        " Check the other constants cl_abap_datadescr=>typekind_* for all the other types
        " you might encounter in your use-case and which might require special treatment.
        WHEN OTHERS.
          " Probably just copyable. If not, you will get a runtime error here and need 
          " to implement special handling for this particular type_kind.
          <lv_out> = <lv_in>.
      ENDCASE.
    ENDIF.
  ENDIF.
ENDLOOP.
岁月静好 2025-02-19 22:43:48

实际上,我更喜欢飞利浦解决方案,在类型处理方面,它更具错误和全面,但仅出于多样性的目的,我将添加这个快速的dirty解决方案。

您可以使用cl_rsda_csv_converter辅助类的方法:

DATA: input TYPE TABLE OF string.

TYPES t_itab TYPE TABLE OF pa0009 WITH EMPTY KEY.
DATA(dref) = NEW t_itab(  ).

APPEND ` 800;90051099;0;;;99991231;20080501;000;20100312;HORVATLU;;;;;;;;;;;;;;;46456.89;HUF;0.00;;0;;;;;HU;;;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.
APPEND ` 800;99000005;0;;;99991231;20170101;000;20170220;GUNASHMA;;;;;;;;;;;;;;;5564665.00;EUR;0.00;;0;;;;;DE;28511111;123;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.

DATA: ls_line TYPE LINE OF t_itab.
LOOP AT input ASSIGNING FIELD-SYMBOL(<fs_s>).
* creating converter
  DATA(lo_csv) = cl_rsda_csv_converter=>create( i_separator = ';' ).
* Process records
  lo_csv->csv_to_structure( EXPORTING i_data = <fs_s> IMPORTING e_s_data = ls_line ).
* inserting into itab
  INSERT ls_line INTO TABLE dref->*.
ENDLOOP.

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:

DATA: input TYPE TABLE OF string.

TYPES t_itab TYPE TABLE OF pa0009 WITH EMPTY KEY.
DATA(dref) = NEW t_itab(  ).

APPEND ` 800;90051099;0;;;99991231;20080501;000;20100312;HORVATLU;;;;;;;;;;;;;;;46456.89;HUF;0.00;;0;;;;;HU;;;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.
APPEND ` 800;99000005;0;;;99991231;20170101;000;20170220;GUNASHMA;;;;;;;;;;;;;;;5564665.00;EUR;0.00;;0;;;;;DE;28511111;123;;;;;;;;;;;;;;00;;;;00000000;;;;; ` TO input.

DATA: ls_line TYPE LINE OF t_itab.
LOOP AT input ASSIGNING FIELD-SYMBOL(<fs_s>).
* creating converter
  DATA(lo_csv) = cl_rsda_csv_converter=>create( i_separator = ';' ).
* Process records
  lo_csv->csv_to_structure( EXPORTING i_data = <fs_s> IMPORTING e_s_data = ls_line ).
* inserting into itab
  INSERT ls_line INTO TABLE dref->*.
ENDLOOP.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文