Application Development and Automation 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: 
Read only

Counter in For iteration with WHERE clausule

marianozena
Explorer
1,532

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_acgl_accountalloc_nmbrcostcenteritem_textfunc_area
1xxxxxxxxxxxxxxxxx
2xxxxxxxxxxxxxxxx

Kind Regards

Mariano

6 REPLIES 6
Read only

Ryan-Crosby
Active Contributor
0 Kudos
1,504

I would think the INDEX specification would be applicable here.

Screenshot 2024-04-12 at 13.22.39.png

Read only

0 Kudos
1,493

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

Read only

0 Kudos
1,487

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.

Read only

Bharathi_S
Explorer
1,387

Hi @marianozena ,

Can you try like this

itemno_acc = lines( gt_accogl ) + 1

 

Read only

0 Kudos
1,139

Many thanks @Sandra_Rossi & @Bharathi_S !!
Regards

Mariano

Read only

Sandra_Rossi
Active Contributor
1,344

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 ).