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: 

New Syntax - update internal table column based on comparision of two columns of same itab

mehul_mori
Explorer
0 Kudos

I have an internal table itab with 4 coulmns col1, col2, col3, col4.

I want to update col3 value in all records by comparing col1 and col2. i.e. if col1 <> col2 then col3 = 'X' else col3 = ' '.

I want to update col4 value in call records with difference of col1 and col2. i.e. col4 = col1 - col2.

Traditionally, it can be achieved with this code:

loop at itab assigning <fs>.
if <fs>-col1 NE <fs>-col2.
<fs>-col3 = 'X'.
endif.
<fs>-col4 = <fs>-col1 - <fs>-col2.
endloop

. Is there a way in new abap syntax to do it without loop and without using second internal table ?

11 REPLIES 11

FredericGirod
Active Contributor
0 Kudos
itab = value #( for my_line in itab ( col_1 = my_line-col_1
                                      col_2 = my_line-col_2
                                      col_4 = ( my_line-col_1 - my_line-col_2 ) ) ).

should be something like this

0 Kudos

Thanks for the answer.

Tried it . It clears the whole table (deletes all rows).

0 Kudos
data(itab2) = value xxx( for my_line in itab ( col_1 = my_line-col_1
                                               col_2 = my_line-col_2
                                               col_4 = ( my_line-col_1 - my_line-col_2 ) ) ).
itab = itab2 

it works in 2 steps .. sorry

0 Kudos

Yes . it does with 2 steps...


Any idea about updating COL3 ?

forget this col_3 🙂

DATA(itab2) = VALUE tt_type_1( FOR my_line IN itab ( col_1 = my_line-col_1
                                                     col_2 = my_line-col_2
                                                     col_3 = cond #( when my_line-col_1 ne my_line-col_2 then abap_true else abap_false )
                                                     col_4 = ( my_line-col_1 - my_line-col_2 ) ) ).

frdric.girod in one step:

data(itab) = value xxx( 
     let itab2 = itab IN
     for my_line in itab2 ( col1 = my_line-col1
                            col2 = my_line-col2
                            col3 = COND #( WHEN my_line-col1 <> my_line-col2 THEN 'X' ELSE my_line-col3 )
                            col4 = COND #( WHEN my_line-col1 =  my_line-col2 THEN my_line-col1 - my_line-col2 ELSE my_line-col4 ) ).

I was waiting for you sandra.rossi about this question 🙂 I think you could answer directly, you solutilon is better (if you add COL_3 !!!)

0 Kudos

previous answer updated...

0 Kudos

Thank you frdric.girod , sandra.rossi

0 Kudos

Ok, now, frdric.girod can update the original answer with the nicest code, can he?

😉

0 Kudos

Noooo we have a nice story 🙂

And Sandra has better proposal, mine was really quick & dirty