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: 

New ABAP syntax, VALUE FOR skip appending entry if it already exists

KT232
Explorer
0 Kudos
538

Hello experts,

 I have the following case - this piece of code is being called inside of a loop where I am being given several entries for each key. On the first iteration we go through all the entries and everything is okay. But the program is a dynpro so when the user interacts with it somehow we restart the loop and we go over the same entries again. So my goal is to prevent that and just keep the entries from the first run, if we go over the entries again with the same key then we just need to update the values with the new ones. Is it still possible with the new syntax?

What I am trying is:

r_unduplicated_values = VALUE #( FOR row_of_tab_with_duplicates IN tab_with_duplicates
                                                        ( CORRESPONDING #( row_of_tab_with_duplicates ) ) .

The closest thing I was able to do was:
r_unduplicated_values = VALUE #( FOR row_of_tab_with_duplicates IN tab_with_duplicates
                                                       ( COND #( WHEN NOT line_exists( r_unduplicated_values[ key = row_of_tab_with_duplicates-key ] )
THEN CORRESPONDING #( row_of_tab_with_duplicates ) ) ) ).

 

But unfortunately this will just append empty rows in the case that the line already exists in the table

What do I need to add to this syntax skeleton so that when a row has already been added like the one we are currently processing in the FOR loop it skips appending this row and continues with the other rows?

I know it is possible to just let it append the duplicates and then sort + delete duplicates but I wanted to do it all at once if possible with the new syntax.

 

Thank you!

5 REPLIES 5

MarcoK
Participant
432

If your table tab_with_duplicates is a sorted table you can try this (I renamed the column key to key_col):

 

 

r_unduplicated_values = VALUE #(
    FOR row_of_tab_with_duplicates IN tab_with_duplicates
    ( LINES OF COND #( WHEN lines(
                           FILTER #( tab_with_duplicates USING KEY primary_key
                           WHERE key_col = row_of_tab_with_duplicates-key_col ) ) > 1
                       THEN VALUE #( ( row_of_tab_with_duplicates ) ) ) ) ).

 

 

0 Kudos
404

 

Unfortunately it is not a sorted table and I do not have an option to change it. Is it still possible?

Also in your syntax it does not let me use FILTER after the lines( ).

Also I tried to reenact the realistic case but I realize now that maybe I didn't explain it very well.

In the real case this piece of code is being called inside of a loop where I am being given several entries for each key. On the first iteration we go through all the entries and everything is okay. But the program is a dynpro so when the user interacts with it somehow we restart the loop and we go over the same entries again. So my goal is to prevent that and just keep the entries from the first run, if we go over the entries again with the same key then we just need to update the values with the new ones. Is it still possible with the new syntax?


r_unduplicated_values = VALUE #( FOR row_of_tab_with_duplicates IN tab_with_duplicates
                                                       ( COND #( WHEN entry doesnt exist in OUR table r_unduplicated_values

                                                                        THEN add it to the r_unduplicated_values

                                                                       ELSE Update the values with the same key in r_unduplicated_Values)

ThorstenHoefer
Active Contributor
375

Hi,
you should use the group function:

CLASS zcl_test_abap IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    TYPES: BEGIN OF ty_data,
             grp  TYPE c LENGTH 4,
           END OF ty_data,
           tty_data TYPE STANDARD TABLE OF ty_data WITH EMPTY KEY.

    FINAL(lt_data) = VALUE tty_data(
          ( grp = 'grp1' )
          ( grp = 'grp1' )
          ( grp = 'grp1' )
          ( grp = 'grp2' )
          ( grp = 'grp2' )
          ( grp = 'grp3' )
          ( grp = 'grp3' ) ).


     data(lt_result) = VALUE tty_data(
       FOR GROUPS <wa_group> OF <wa_data> IN lt_data
            GROUP BY  <wa_data>
            ( corresponding #( <wa_group> ) ) ).

    out->write( name = 'result' data = lt_result[] ).
  ENDMETHOD.
ENDCLASS.




0 Kudos
366

Hello, I have tested the code but unfortunately the reality is a bit different than what I described and it doesn't work. In the real case this piece of code is being called inside of a loop where I am being given several entries for each key. On the first iteration we go through all the entries and everything is okay. But the program is a dynpro so when the user interacts with it somehow we restart the loop and we go over the same entries again. So my goal is to prevent that and just keep the entries from the first run, if we go over the entries again with the same key then we just need to update the values with the new ones. Is it still possible with the new syntax?

KT232
Explorer
0 Kudos
265

This is still relevant for me if someone has idea