2025 May 07 11:04 AM - edited 2025 May 07 11:08 AM
Dear Team,
How to convert the below code into sap new syntax
LOOP AT it_h1 INTO wa_h1n WHERE regiogroup = 'S01' AND supply_division+0(1) = 'W'.
IF wa_h-langu_corr = 'M2'.
LOOP AT it_swm INTO lv_string WHERE opbel_bi = wa_h1n-opbel_bi.
TRANSFER lv_string TO p_file.
CLEAR lv_string.
ENDLOOP.
ELSE.
LOOP AT it_swe INTO lv_string WHERE opbel_bi = wa_h1n-opbel_bi.
TRANSFER lv_string TO p_file.
CLEAR lv_string.
ENDLOOP.
ENDIF.
ENDLOOP.
2025 May 07 12:25 PM - edited 2025 May 07 12:27 PM
Why do you think it's worth converting?
The only question I see by reading your code, concerning performance, is how many lines are in IT_H1, IT_SWM and IT_SWE, and if they are defined with a sorted key respectively, regiogroup, opbel_bi and opbel_bi.
NB: "lv_string" has a meaningless name and should be renamed + there are also two useless lines "CLEAR lv_string".
2025 May 08 5:29 AM
Thank your for your quick reply. All three tables contain more than 1,000 records, and the internal tables are sorted by key. The above code is executed under the OPEN DATASET command to write a file to the application server, which is why we are transferring the table data to p_file, which is of type string.
We are experiencing performance issues, so we are trying to improve performance by using modern inline syntax.
2025 May 10 11:26 PM
those are just syntax sugar, code may look succinct, but it doesn't mean high performance.
2025 May 11 8:49 AM - edited 2025 May 11 9:11 AM
If the statement "sorted by key" means a standard internal table followed by SORT, then LOOP AT is not optimized at all (only READ TABLE with BINARY SEARCH is optimized).
"LOOP AT" can be optimized only if the internal table has a "sorted index" i.e. it's declared with "TYPE SORTED TABLE" (sorted primary index).
Another alternative is to use a sorted secondary index declared for the internal table via "WITH ... SORTED KEY seckey COMPONENTS", and to use "LOOP AT itab USING KEY seckey WHERE ..."
Note that "supply_division+0(1) = 'W' " cannot be optimized before ABAP 7.58 (maybe needs to be rewritten "supply_division BETWEEN 'W' and 'WZZZ' "), so I hope that using an index for "regiogroup = 'S01' " discriminates the lines sufficiently.
2025 May 10 7:10 PM
Please check out the project "ABAPCleaner" (link) and the power of using it to refactor / modernize your code : 🟢 How to Clean your ABAP code in Seconds with ABA... - SAP Community
Install ABAP Developer Tools (ADT) and use the ABAPCleaner to get hints on how to optimize the code.
If it doesn't offer you a good hint on optimization, it's probably not necessary ..
Hope this helps
Nic T.