Problem Statement :
How to Swap 2 Elements of an Array :
Input :
Output :
Prerequisites :
- Azure Data Factory / Synapse
Solution :
GitHUB Code
In Synapse / ADF , it is not possible to self reference a variable in Set Variable Activity. Hence, we would need to create a TempArray variable and then over ride the original array with the TempArray one.
- To Swap the elements in an array, we can follow the below option of recreating a new array from the Initial Array :
- a) Get elements from 0 index till N-1 Index , where N is the Minimum Index (Elements from Left side of Minimum Index)
- b) The Maximum Index position Element would be Element in the Minimum Index Position Element.
- c) Get elements in between the Minimum Index and the Maximum Index (Middle part between both Index)
- d) The Minimum Index position Element would be Element in the Maximum Index Position Element.
- e) Get elements from M+1 index till end, where M is the Maximum Index (Elements from Right side of Maximum Index)
- To get Elements from Left side of Minimum Index (#a)
@take(variables('InputArray'), pipeline().parameters.SwapIndex1)
Result :
3. To get Middle part between both Index (#c)
@take(skip(variables('InputArray'),add(pipeline().parameters.SwapIndex1,1)),sub(pipeline().parameters.SwapIndex2,add(pipeline().parameters.SwapIndex1,1)))
Result :
4. To get Elements from Right side of Maximum Index (#e)
@skip(variables('InputArray'), add(pipeline().parameters.SwapIndex2,1))
Result :
5. To generate an Array based on #a to #e mentioned in #1
@createArray(if(empty(variables('LeftHalf')),'',variables('LeftHalf')),variables('InputArray')[pipeline().parameters.SwapIndex2],if(empty(variables('Middle')),'',variables('Middle')),variables('InputArray')[pipeline().parameters.SwapIndex1],if(empty(variables('RightHalf')),'',variables('RightHalf')))
Result :
6. To Override the InputArray variable and get the Array in required format :
@json(concat('[',replace(replace(replace(replace(string(variables('TempArray')), '[', ''),']',''),'"",',''),'""',''),']'))
Final Result :
Note : For below scenario
Results :
If we use the below expression in TempArray Set Variable Activity :
@createArray(variables('LeftHalf'),variables('InputArray')[pipeline().parameters.SwapIndex2],variables('Middle'),variables('InputArray')[pipeline().parameters.SwapIndex1],variables('RightHalf')))
Final Output :
So to avoid the null value, we have to manage the Empty Arrays in case if any for LeftHalf, Middle and RightHalf variables by replacing it Empty Array with ” as with below code in TempArray activity.
@createArray(if(empty(variables('LeftHalf')),'',variables('LeftHalf')),variables('InputArray')[pipeline().parameters.SwapIndex2],if(empty(variables('Middle')),'',variables('Middle')),variables('InputArray')[pipeline().parameters.SwapIndex1],if(empty(variables('RightHalf')),'',variables('RightHalf')))
Output :
And Replace “”, & “” with blank in Final activity
Output :