Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, August 21, 2019 1:20 PM
I need to implement a way of finding the iteration number in a foreach loop.
This is to enable me to access elements of an array generated by the Get Metadata activity
Is there any property of the for each activity I can access which would give me this information.
I cannot use as variable as you cannot reference the variable in the set variable expression.
All replies (3)
Wednesday, August 21, 2019 10:11 PM
Hello amie.barkes and thank you for your inquiry. I am familiar with this limitation, and have a work-around. Before I go into the work-around I would like to make a suggestion.
Suppose that the ForEach is iterating over that array generated by the Get Metadata activity. To access the item of the current iteration, the expression "@item()" is used. If it is an array of objects, you might need to use something like "@item().value".
That said, there is a roundabout way to implement a counter, but it will only work when the "Sequential" setting of ForEach activity is used. This is because, by default, Data Factory tries to run things in parallel. If "Sequential" is not used, then items will execute out of order.
The restriction we need to get around, disallows "A = A + 1". The work-around is to do:
- (initialize counter_var variable)
- temporary_var = counter_var + 1
- counter_var = temporary_var
Here is the code for an example.
{
"name": "ForEachCounter",
"properties": {
"activities": [
{
"name": "ForEach4",
"type": "ForEach",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"items": {
"value": "@variables('list')",
"type": "Expression"
},
"isSequential": true,
"activities": [
{
"name": "Add",
"type": "SetVariable",
"dependsOn": [],
"userProperties": [],
"typeProperties": {
"variableName": "temp",
"value": {
"value": "@{add(int(variables('counter')),1)}",
"type": "Expression"
}
}
},
{
"name": "Set Counter",
"type": "SetVariable",
"dependsOn": [
{
"activity": "Add",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "counter",
"value": {
"value": "@variables('temp')",
"type": "Expression"
}
}
}
]
}
},
{
"name": "Show final value",
"type": "SetVariable",
"dependsOn": [
{
"activity": "ForEach4",
"dependencyConditions": [
"Succeeded"
]
}
],
"userProperties": [],
"typeProperties": {
"variableName": "Final",
"value": {
"value": "@variables('counter')",
"type": "Expression"
}
}
}
],
"variables": {
"list": {
"type": "Array",
"defaultValue": [
"A",
"B",
"C",
"D"
]
},
"counter": {
"type": "String",
"defaultValue": "0"
},
"temp": {
"type": "String"
},
"Final": {
"type": "String"
}
},
"annotations": []
}
}
There are other solutions as well, such as using an until loop where you pop elements off the array.
Thursday, August 22, 2019 9:12 PM
@amie.barkes I haven't heard back. Did my suggestion help you?
Friday, August 23, 2019 5:31 PM
since I haven't heard back I will assume either my suggested helped you, or you found your own solution.