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: 

Last entry of loop not appending

kmersin95
Explorer
0 Kudos
289

Hello everyone,

because I am new to abap and sap in general (3 weeks), I've got a question regarding the following code. The problem is that only 5 of the 6 in total sap_objects get appended to lt_results, so I need a syntax or whatever that states that the last sap_object gets appeneded as well.
Does anyone know how to do that?
Most of the code was done with a colleague that teaches me but for this specific problem I am left on my own and I coulndt find anything else on the internet to that. Let me know if any info might be missing.
Thanks in advance!


SORT lt_toaAll BY sap_object.

LOOP AT lt_toaALL INTO ls_toaALL.
IF sy-tabix = 1.
sap_object = ls_toaAll-sap_object.
index = index + 1.
ELSEIF sap_object = ls_toaAll-sap_object.
index = index + 1.
ELSE.
ls_result-sap_object = sap_object.
ls_result-sap_object_index = index.
CLEAR: sap_object, index.
sap_object = ls_toaAll-sap_object.
index = index + 1.
APPEND ls_result TO lt_results.
ENDIF.
ENDLOOP.

3 REPLIES 3

FredericGirod
Active Contributor
0 Kudos
235

Your IF is really strange, first you make the condition of the index of the internal table, second on the value of SAP_OBJECT, and the last is other case.

And you do an APPEND in the last case.

Could you change the APPEND position, to do it after the ENDIF ?

kmersin95
Explorer
0 Kudos
235

Yes I tried doing so, but the whole table gets messed up when changing the position after the ENDIF. You can see all the objects on top under "Objekte" except PREL and the question is, how I am able to get it in there because when debugging, I see PREL being counted but just not appended somehow.

From this



to this

thkolz
Contributor
235

You can achieve this quite easily with the new LOOP AT GROUP syntax:

TYPES:
BEGIN OF ty_objects,
sap_object TYPE string,
END OF ty_objects,

BEGIN OF ty_result,
sap_object TYPE string,
sap_object_index TYPE sytabix,
END OF ty_result.

DATA:
lt_toaall TYPE STANDARD TABLE OF ty_objects,
lt_result TYPE STANDARD TABLE OF ty_result.

lt_toaall = VALUE #(
( sap_object = 'BUS2081' )
( sap_object = 'BUS2012' )
( sap_object = 'BUS2081' )
( sap_object = 'BUS2012' )
( sap_object = 'BUS2081' )
( sap_object = 'BUS2012' )
( sap_object = 'BUS2081' )
( sap_object = 'BUS2012' )
( sap_object = 'BUS2081' )
( sap_object = 'BUS2081' )
).

SORT lt_toaall BY sap_object.

LOOP AT lt_toaall INTO DATA(ls_toaall)
GROUP BY ls_toaall-sap_object
INTO DATA(sap_obj).

* Add initial line
APPEND INITIAL LINE TO lt_result ASSIGNING FIELD-SYMBOL(<fs_result>).

* Process group members
LOOP AT GROUP sap_obj ASSIGNING FIELD-SYMBOL(<fs_members>).
<fs_result>-sap_object = <fs_members>-sap_object.
ADD 1 TO <fs_result>-sap_object_index.
ENDLOOP.

ENDLOOP.

* Display result
cl_demo_output=>display( lt_result ).

Merry Christmas!