清除动态内部表中某一列为空值的行。

huangapple go评论64阅读模式
英文:

Clearing rows of dynamic internal table with empty value in one column

问题

我正在尝试删除具有一个名为 segment 的列中为空值的行。该表是动态声明的,因此我无法像以前一样在DATA声明中使用标准类型(如DATA table_x TYPE BSEG)。

我尝试创建一个基于导入表的结构,但是我收到了一个错误消息:
> 数据对象“LS_DYN_TABLE”没有结构,因此没有名为“SEGMENT”的组件。

是否有一种动态声明结构的方式,以便我可以循环遍历表格,删除行并将过滤后的表格传递给其他地方?

DATA:
  dy_table          TYPE REF TO data.

FIELD-SYMBOLS:
  <dyn_table>  TYPE STANDARD TABLE,
  <fs_segment> TYPE any.

IF ID  = '1234'.

  me->method_that_import_desired_table( IMPORTING et_table = dy_table ).
  ASSIGN dy_table->* TO <dyn_table>.
  DATA(ls_dyn_table) = dy_table.
  LOOP AT <dyn_table> ASSIGNING FIELD-SYMBOL(<fs_row>).
    IF ls_dyn_table-segment IS INITIAL.
      CONTINUE.
    ENDIF.
  ENDLOOP.
ENDIF.
英文:

I am trying to remove rows that have empty values in one column segment. The table is declared dynamically so it does not have a standard type that I can use in DATA declaration like I used to do (DATA table_x TYPE BSEG).
I tried to create structure based of the imported table but I receive an error:
> The data object "LS_DYN_TABLE" does not have a structure and therefore does not have a component called "SEGMENT"

Is there any way to declare dynamically a structure so I can loop through a table, remove rows and pass filtered table further?

    DATA:
      dy_table          TYPE REF TO data.


    FIELD-SYMBOLS:

      &lt;dyn_table&gt;  TYPE STANDARD TABLE,
      &lt;fs_segment&gt; TYPE any.

    IF ID  = &#39;1234&#39;.


      me-&gt;method_that_import_desired_table( IMPORTING et_table = dy_table ).
      ASSIGN dy_table-&gt;* TO &lt;dyn_table&gt;.
      DATA(ls_dyn_table) = dy_table.
      LOOP AT &lt;dyn_table&gt; ASSIGNING FIELD-SYMBOL(&lt;fs_row&gt;).
        
        IF ls_dyn_table-segment IS INITIAL.
          CONTINUE.
        ENDIF.
      ENDLOOP.
    ENDIF.

答案1

得分: 0

在循环内部表时,您必须动态分配字段段并检查它是否具有值:

LOOP AT &lt;dyn_table&gt;
     ASSIGNING FIELD-SYMBOL(&lt;fs_row&gt;).
  DATA(tabix) = sy-tabix. "注意内部表中的行索引
  ASSIGN COMPONENT 'SEGMENT'
         OF STRUCTURE &lt;fs_row&gt;
         TO FIELD-SYMBOL(&lt;segment&gt;). 
  IF sy-subrc EQ 0 AND
     &lt;segment&gt; IS INITIAL.
    DELETE &lt;dyn_table&gt; INDEX tabix. "按行索引删除
  ENDIF.
ENDLOOP.
英文:

When you loop the internal table, you have to assign the field segment dynamically and check if it has a value:

LOOP AT &lt;dyn_table&gt;
     ASSIGNING FIELD-SYMBOL(&lt;fs_row&gt;).
  DATA(tabix) = sy-tabix. &quot;note row index in internal table
  ASSIGN COMPONENT &#39;SEGMENT&#39;
         OF STRUCTURE &lt;fs_row&gt;
         TO FIELD-SYMBOL(&lt;segment&gt;). 
  IF sy-subrc EQ 0 AND
     &lt;segment&gt; IS INITIAL.
    DELETE &lt;dyn_table&gt; INDEX tabix. &quot;delete by row index
  ENDIF.
ENDLOOP.

huangapple
  • 本文由 发表于 2023年7月13日 20:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定