简体   繁体   English

FOR 表达式和 let 表达式过滤内表

[英]FOR expression and let expression to filter an internal table

I coded the following line of code我编写了以下代码行

DATA(lt_heads_ok) = VALUE my_head_table( for wa IN g_heads
                      LET ok = g_model->is_head_ok( wa-id )
                      IN ( COND #(  WHEN ok = abap_true THEN wa ) ) ).

I can activate it but the results seems weird to me.我可以激活它,但结果对我来说似乎很奇怪。 Indeed I get all the lines but empties are none of them are valid according to my conditions.事实上,我得到了所有的线条,但根据我的条件,它们都不是有效的。

Is there a way to avoid to append an empty line when it does not match the "COND" condition ?有没有办法避免在与“COND”条件不匹配时附加空行?

Adding lines conditionally in a FOR iteration can be done in two ways.在 FOR 迭代中有条件地添加行可以通过两种方式完成。 Note that the same question arises even if LET is not used.请注意,即使不使用LET ,也会出现同样的问题。

The first way is to use LINES OF :第一种方法是使用LINES OF

CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS is_ok IMPORTING sflight TYPE sflight RETURNING VALUE(result) TYPE abap_bool.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD is_ok.
    IF sflight-seatsmax - sflight-seatsocc > 10. result = abap_true. ENDIF.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  TYPES ty_sflights TYPE STANDARD TABLE OF sflight WITH DEFAULT KEY.

  SELECT * FROM sflight INTO TABLE @DATA(sflights).

  DATA(sflights_filtered) = VALUE ty_sflights( 
        FOR <sflight> IN sflights
        ( LINES OF COND #( 
              WHEN lcl_app=>is_ok( <sflight> ) = abap_true 
              THEN VALUE #( ( <sflight> ) ) ) ) ).

The second way is to use REDUCE :第二种方法是使用REDUCE

  DATA(sflights_filtered) = REDUCE #( 
        INIT aux_sflights TYPE ty_sflights
        FOR <sflight> IN sflights
        NEXT aux_sflights = COND #( 
              WHEN lcl_app=>is_ok( <sflight> ) = abap_true 
              THEN VALUE #( BASE aux_sflights ( <sflight> ) )
              ELSE aux_sflights ) ).

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

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