‎2021 Mar 03 4:55 AM
Hello Experts,
Please help me how i can write below type of Loop AT statement as per 7.4 syntax.
Loop at lt_items into data(ls_items) where matnr = hzd456 and erdat le sy-datum .
if ls_items-sequence = 1.
lv_found = 'X'.
exit.
else.
clear : ls_items.
Endif.
endloop.
lv_value = ls_items-value.
‎2021 Mar 03 6:20 AM
As you have a LE condition no usage of a table expression is possible. So an adapted loop is a good option from my point of view (consider to use e.g. a sorted table when you have a lot of data in the internal table):
LOOP AT lt_items REFERENCE INTO DATA(lr_item)
WHERE matnr = 'hzd456'
AND erdat le sy-datum
AND sequence = 1.
lv_value = lr_item->value.
EXIT.
ENDLOOP.Of course you could write something like following, but I see not real advantage in that:
DATA(lt_filtered_items) = FILTER #( lt_items
WHERE matnr = 'hzd456' AND erdat le sy-datum AND sequence = 1 ).
TRY.
lv_value = lt_filtered_items[ 1 ]-value.
CATCH cx_sy_itab_line_not_found.
" exception handling
ENDTRY.
‎2021 Mar 03 6:20 AM
As you have a LE condition no usage of a table expression is possible. So an adapted loop is a good option from my point of view (consider to use e.g. a sorted table when you have a lot of data in the internal table):
LOOP AT lt_items REFERENCE INTO DATA(lr_item)
WHERE matnr = 'hzd456'
AND erdat le sy-datum
AND sequence = 1.
lv_value = lr_item->value.
EXIT.
ENDLOOP.Of course you could write something like following, but I see not real advantage in that:
DATA(lt_filtered_items) = FILTER #( lt_items
WHERE matnr = 'hzd456' AND erdat le sy-datum AND sequence = 1 ).
TRY.
lv_value = lt_filtered_items[ 1 ]-value.
CATCH cx_sy_itab_line_not_found.
" exception handling
ENDTRY.
‎2021 Mar 03 8:02 AM
Why isn't it possible with LE / <= ? (there's only an issue with EXIT for which the only workaround I know is to use a THROW exception):
I didn't test it, something like that:
TRY.
DATA(lv_value) = REDUCE #(
INIT v TYPE ...
exit TYPE i
FOR ... WHERE ( ... erdat <= sy-datum ... )
NEXT v = ...
exit = COND #( WHEN v IS NOT INITIAL THEN THROW lx_stop ) )
CATCH lx_stop.
" Stop
ENDTRY.of course that's a stupid code 😛
‎2021 Mar 03 8:04 AM
Sure, with a REDUCE expression it is possible - not really readable on a first glimpse, but it works.
I talked about table expressions.