Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

EXIT Continue FOR IN Loop Statement

rico_stoll
Explorer
0 Kudos
2,625

Hi Experts,i try a VALUE Statement in ABAP with a FOR Loop to build a new table. FILTER Statement don#t fit for me, because declaring table keys for internal tables struggles me alot. so let's say thats not an Option.

I tried different variants of creating my internal table with a FOR Loop to see whats going on. Last Statement withound an ELSE Condition adds a empty row every time condition not Fitting.

So how can I Prevent to append an empty row? I thought About adding CONTINUE key word, but Syntax check dont allow.

My Question: is it possible to Exit this FOR IN Loop conditionaly?

SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

TYPES: BEGIN OF ly_table,

         flag        TYPE boole_d,

         category_id TYPE zdb_psets-category_id,

         text        TYPE string,

       END OF ly_table.



DATA lv_category TYPE zdb_psets-category_id VALUE '19210000'.

DATA lt_table TYPE TABLE OF ly_table.

DATA lt_table_filtered TYPE TABLE OF ly_table.

data empty_line like line of lt_table.



"create table:

lt_table = VALUE #( ( flag = abap_true category_id = '19*' text = 'blabla' ) ( flag = abap_true category_id = '1922' text = 'hello world' ) ).

IF lt_table IS INITIAL.

  RETURN.

ENDIF.



*"filter table

lt_table_filtered = VALUE #( FOR wa IN lt_table WHERE ( flag EQ abap_true ) ( wa ) ).

*

BREAK-POINT.

*

"this adds a empty line if condition not fit. not bad but not good

lt_table_filtered = VALUE #( FOR wa IN lt_table WHERE ( flag EQ abap_true ) ( cond #( WHEN lv_category CP wa-category_id then wa else empty_line ) ) ).

*

BREAK-POINT.



"without else condition

lt_table_filtered = VALUE #( FOR wa IN lt_table WHERE ( flag EQ abap_true ) ( cond #( WHEN lv_category CP wa-category_id then wa ) ) ).



BREAK-POINT.

1 ACCEPTED SOLUTION

rico_stoll
Explorer
1,437

thanks sandra.rossi
it takes a bit to set the brekets Right. But now it works without adding empty lines.

I didnt know about LINES OF COND Statement.


sandra.rossi cant reward you, cause your answer is a comment. 😞

So this is the working coding:

"do not add empty lines

lt_table_filtered = VALUE #( FOR wa IN lt_table WHERE ( flag EQ abap_true )

( LINES OF cond #( WHEN lv_category CP wa-category_id then VALUE #( ( wa ) ) ) )

).
2 REPLIES 2

Sandra_Rossi
Active Contributor
1,437

I didn't look at your question precisely, but if you want to add either one line or zero line at each loop, one workaround is to use LINES OF (either an itab of 1 line or of 0 line). Dumb example to take only lines with SEATSOCC < SEATSMAX:

SELECT * FROM sflight INTO TABLE @DATA(flights).
TYPES ty_flights TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
DATA(flights_with_seats_left) = VALUE ty_flights( FOR <flight> IN flights
                ( LINES OF COND #( WHEN <flight>-seatsocc LT <flight>-seatsmax
                                   THEN VALUE #( ( <flight> ) ) ) ) ).

rico_stoll
Explorer
1,438

thanks sandra.rossi
it takes a bit to set the brekets Right. But now it works without adding empty lines.

I didnt know about LINES OF COND Statement.


sandra.rossi cant reward you, cause your answer is a comment. 😞

So this is the working coding:

"do not add empty lines

lt_table_filtered = VALUE #( FOR wa IN lt_table WHERE ( flag EQ abap_true )

( LINES OF cond #( WHEN lv_category CP wa-category_id then VALUE #( ( wa ) ) ) )

).