2024 Apr 12 6:05 PM
Hi,
I'm trying to populate a table using new syntax
I wonder to know how to populate a pos number field increasing it in 1 unit per each iteration. I can't use index because the FOR iteration has WHERE clause.
This is my code
gt_accogl = VALUE #( FOR ls_outtab
IN gt_outtab
WHERE ( bldat = gs_outtab-bldat
AND lifnr = gs_outtab-lifnr )
(
itemno_acc = ????
gl_account = |{ ls_outtab-hkont ALPHA = IN }|
alloc_nmbr = ls_outtab-zuonr
costcenter = ls_outtab-kostl
item_text = ls_outtab-sgtxt
func_area = ls_outtab-fkber
) ).
I'm specting this result....
itemno_ac | gl_account | alloc_nmbr | costcenter | item_text | func_area |
1 | xxx | xxxx | xxxx | xxx | xxx |
2 | xxx | xxxx | xxx | xxx | xxx |
Kind Regards
Mariano
2024 Apr 12 6:23 PM
I would think the INDEX specification would be applicable here.
2024 Apr 12 6:47 PM
Hi @Ryan-Crosby
Thanks for your answer but unfortunately is not applicable. INDEX is returning the position of the record in the source table.
Regards
2024 Apr 12 7:02 PM
The only other option would be using a LET expression for local helper variables, but inside the help it mentions that sy-tabix is not set by a FOR expression and suggests using INDEX INTO, which already didn't seem to work for what you are trying to do.
2024 Apr 13 4:47 AM
2024 Apr 16 12:21 PM
2024 Apr 13 1:13 PM
Same as @Bharathi_S but with an example reproducible by anyone:
TYPES:
BEGIN OF ts_itab,
field TYPE string,
END OF ts_itab.
TYPES tt_itab TYPE STANDARD TABLE OF ts_itab WITH EMPTY KEY.
DATA(gt_outtab) = VALUE tt_itab( ( field = `1` )
( field = `2` )
( field = `3` ) ).
DATA(gt_accogl) = VALUE tt_itab( ).
gt_accogl = VALUE #( FOR <ls_outtab> IN gt_outtab INDEX INTO index
WHERE ( field <> `2` )
( field = |{ <ls_outtab>-field } : itemno_acc = { LINES( gt_accogl ) + 1 }, index = { index }| ) ).
ASSERT gt_accogl = VALUE tt_itab( ( field = `1 : itemno_acc = 1, index = 1` )
( field = `3 : itemno_acc = 2, index = 3` ) ).
A more general solution would be to use REDUCE + LET ... IN:
DATA(gt_accogl) = REDUCE #( INIT itab = VALUE tt_itab( )
itemno_acc = 1
FOR <ls_outtab> IN gt_outtab INDEX INTO index
WHERE ( field <> `2` )
NEXT itab = VALUE #(
BASE itab
( field = |{ <ls_outtab>-field } : itemno_acc = { itemno_acc }, index = { index }| ) )
itemno_acc = itemno_acc + 1 ).