简体   繁体   English

如何在内部表中生成新的自己的列表? 阿巴

[英]How to generate new own list in an internal table? abap

List in the internal table at the beginning:在开头的内表中列出:

code | name | sum
 22  | Jon  | 234.3
 22  | Jon  | 34.2
 22  | Jon  | 0
 22  | Jon  | 0
 12  | Bob  | 999.4
 12  | Bob  | 0
 45  | Anna | 0
 45  | Anna | 0
 11  | Mike | 0
 11  | Mike | 234.3

To get the output of such a list from the internal table:要从内部表中获取此类列表的输出:

code | name | sum
 22  | Jon  | 234.3
 22  | Jon  | 34.2
 12  | Bob  | 999.4
 45  | Anna | 0
 11  | Mike | 234.3

Conditions for the formation of a new (outgoing) list:形成新(传出)清单的条件:

  1. If the column name with several identical names (for example with four - Jon ) has a value (column sum ) 34.5 ... and 0, then throw away all 0 and print only non-zero.如果有几个相同的名称(例如四-乔恩)列有一个值(列总和)34.5 ...和0,然后扔掉所有0并打印非零。
  2. If the column name with several identical names (for example two - Anna ) has a value (column sum ) 0 - then print only one name with the value 0.如果具有几个相同的名称(例如两个-安娜)列具有值(列总和)0 -然后用值0仅打印一个名称。
  3. The list can not be sorted - the output must have a list with the same order as the input.列表无法排序 - 输出必须有一个与输入顺序相同的列表。

I'm considering lt_grp1 will contains beginning internal table records.我正在考虑lt_grp1将包含开始的内部表记录。 And i've declared lt_grp2 of same structure as lt_grp1 .我已经声明了与lt_grp2具有相同结构的lt_grp1

  DATA: lv_index TYPE i VALUE 0.

  APPEND LINES OF lt_grp1 TO lt_grp2.
  DELETE ADJACENT DUPLICATES FROM lt_grp2 COMPARING code name.

  LOOP AT lt_grp2 INTO ls_grp1.

    LOOP AT lt_grp1 INTO ls_grp2
          WHERE code = ls_grp1-code
            AND name = ls_grp1-name.
      lv_index = lv_index + 1.
      IF ls_grp2-sum = 0.
        IF lv_index > 1.
          DELETE lt_grp1 INDEX sy-tabix.
        ENDIF.
      ELSE.
        IF lv_index > 1.
          DELETE lt_grp1 WHERE sum = 0
                          AND code = ls_grp1-code.
        ENDIF.
      ENDIF.
      CLEAR: ls_grp2.
    ENDLOOP.
    CLEAR : lv_index.
  ENDLOOP.


  CLEAR :ls_grp1.
  LOOP AT lt_grp1 INTO ls_grp1.
    WRITE: / ls_grp1-code, ls_grp1-name, ls_grp1-sum.
  ENDLOOP.

Hope this helps!希望这可以帮助!

For those who think i didn't tested it.对于那些认为我没有测试过的人。

Here is input table -这是输入表-

输入表

Output -输出 -

在此处输入图片说明

This has O(n log(n)) runtime.这有 O(n log(n)) 运行时。

lt_copy = lt_original.
SORT lt_copy BY code name ASCENDING sum DESCENDING. "if there is a non-zero line, it is at the top "
DELETE ADJACENT DUPLICATES FROM lt_copy.

LOOP AT lt_original INTO DATA(ls_original).
  IF ls_original-sum = 0. "only 0 lines need to be checked"
    READ TABLE lt_copy ASSIGNING <fs_max_sum> BINARY SEARCH
        WITH KEY code = <fs_original>-code
                 name = <fs_original>-name.
    IF <fs_max_sum>-sum = 0.
      "there are only zeros for this code and name, we need it"
    ELSE.
      DELETE lt_original.
    ENDIF.
  ENDIF.
ENDLOOP.

I don't have enough points to comment on someone else's post but I see that they are sorting the tables when the spec said that it must not be sorted. 我没有足够的要点来评论其他人的帖子,但是我看到当规范说它不能排序时,他们正在对表格进行排序。 You are unfortunately going to have to sort this table (which is actually fortunate thing). 不幸的是,您将不得不对该表进行排序(这实际上是幸运的事情)。 You need to ask why it must remain in that specific manner. 您需要问为什么它必须以这种特定方式保留。

I tried an approach with no sorting but I lose Anna when extracting the data. 我尝试了一种不进行排序的方法,但是在提取数据时丢失了Anna。

LOOP At itab.
    READ TABLE itab WITH KEY code = itab-code TRANSPORTING NO FIELDS.
    IF SY-SUBRC EQ 0.
      IF itab-sum NE 0.
        wa-code = itab-code.
        wa-name = itab-name.
        wa-sum = itab-sum.
        APPEND wa.
      ENDIF.
    ENDIF
  ENDLOOP.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM