cancel
Showing results for 
Search instead for 
Did you mean: 

FOR expressions with CORRESPONDING operator and modifying the internal table

user_1234
Explorer
0 Kudos

Hi Experts,

Need your help in analyzing the below code.

My requirement is to create a new internal table( et_item_type) from an existing internal table( lt_item_type) by changing a few fields in the structure.

Please find the below code for reference.

The new table et_item_type should be able to switch the item type when it is 'ZABC' to 'ZXYZ' else it should be 2.

Somehow this logic does not seem to work.

Got my reference from

https://answers.sap.com/questions/12331894/for-expressions-with-corresponding-operator-and-ch.html

TYPES: BEGIN OF lty_itemtype,
v_itemtype TYPE /scdl/dl_itemtype,
v_itemcat TYPE /scdl/dl_itemtype,
END OF lty_itemtype.
DATA: lv_zdlv TYPE /scdl/dl_itemtype VALUE 'ZABC'.
DATA: lv_odlv TYPE /scdl/dl_itemtype VALUE 'ZXYZ'.
DATA : it_item_type TYPE STANDARD TABLE OF lty_itemtype,
et_item_type TYPE STANDARD TABLE OF lty_itemtype.

et_item_type = VALUE #( FOR wa IN it_item_type ( CORRESPONDING #(BASE ( VALUE #( v_itemtype = SWITCH /scdl/dl_itemtype( WHEN lv_odlv THEN lv_zdlv ELSE 2 ) ) ) wa ) ) ).
Sandra_Rossi
Active Contributor
0 Kudos

A Constructor Expression is to create a value, why using it to update a value except to make your program slower?

Accepted Solutions (1)

Accepted Solutions (1)

joltdx
Active Contributor
0 Kudos

Hi!

Well, there's a missing space before BASE, and you got the SWITCH syntax wrong. It's

SWITCH type( operand WHEN const1 THEN result1 ELSE resultn ) 

And note that you can not use variables for the const1, so that's the third thing. Fourth would be that you switched the variables in the SWITCH from how you explain it "when it is 'ZABC' to 'ZXYZ'. They should be the opposite order...

Here's the ABAP Documentation for the SWITCH operator

And then, I can't really get my head around your VALUE #( CORRESPONDING #( BASE ( VALUE #( ) ) ) ) thing... Here's how I would do it, using your variable names. Given I understand what you want to accomplish 🙂

et_item_type = VALUE #( FOR wa IN it_item_type
                        ( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 )
                          v_itemcat = wa-v_itemcat ) ).

With it_item_type:

ZAAA        AAA        
ZABC        BBB        
ZABC        CCC        
ZAAA        DDD        
ZABC        EEE        

You will get et_item_type:

2           AAA        
ZXYZ        BBB        
ZXYZ        CCC        
2           DDD        
ZXYZ        EEE        
user_1234
Explorer
0 Kudos

Thanks for your comment,

I was able to accomplish the functionality with the tweaks you had mentioned.

The reason I need corresponding is that my table has 20 fields and I cannot fill each field in my code.

This does not work

et_item_type = VALUE #( FOR wa IN it_item_type ( CORRESPONDING #( BASE
( value #( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 ) ) )
wa ) )
).

May be SAP does not work on the above syntax, I am not sure, will try to probe further, else I need to go with the conventional method , of looping the table and use corresponding to achieve the end result

This works flawlessly ( as stated by you ) but I cannot fill all the feilds mentioned in the table.

et_item_type = VALUE #( FOR wa IN it_item_type
( v_itemtype = SWITCH #( wa-v_itemtype WHEN lv_zdlv THEN lv_odlv ELSE 2 )
v_itemcat = wa-v_itemcat ) ).
joltdx
Active Contributor
0 Kudos

Well, you can of course, but I see how that's inconvenient.

Anyway, I added an alternative solution using MODIFY itab.

Answers (1)

Answers (1)

joltdx
Active Contributor
0 Kudos

Adding an alternative solution:

et_item_type = it_item_type.
MODIFY et_item_type FROM VALUE #( v_itemtype = lv_odlv ) TRANSPORTING v_itemtype WHERE v_itemtype = lv_zdlv.
MODIFY et_item_type FROM VALUE #( v_itemtype = 2 ) TRANSPORTING v_itemtype WHERE v_itemtype <> lv_odlv.