0
votes

I'm extracting a .txt into an array, line by line.

After doing that, I want to check for a match between a string variable and an element in the array(contains, does not have to be exact match), and return the index of said element.

Is there any solution to this?

I've both tried working with for-each loops and until-loops, but so far the app is iterating until it times out.

2

2 Answers

0
votes

This solution may not be for you but it's an option.

When you process your CSV data, instead of just adding line by line to the array, you could add an object that contains the index of each row (starting from 0). This is an example of what I mean ...

CSV File

This is a test 1 
This is a test 2 
This is a test 3 
This is a test 4

Reference Array Variable (with objects)

[
  {
    "Index": 0,
    "Text": "This is a test 1"
  },
  {
    "Index": 1,
    "Text": "This is a test 2"
  },
  {
    "Index": 2,
    "Text": "This is a test 3"
  },
  {
    "Index": 3,
    "Text": "This is a test 4"
  }
]

Reference Array

You can then filter that array using the Filter array data operation like so ...

Filter Array

Left Side Expression = item()?['Text']

Right Side Expression = string(3) (I'm searching for a string of 3 but because it's a number, I need to treat it slightly differently, it's unlikely you'll have this problem)

That filter produces this result ...

Filtered Array

... from which you can get the index of the first found item ...

Found Index Action

This is the expression ...

if(greater(length(body('Filter_array')), 0), first(body('Filter_array'))['Index'], -1)

... and there you go ...

Found Index Result

This is the JSON definition if you're interested.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Filter_array": {
                "inputs": {
                    "from": "@variables('Reference Array')",
                    "where": "@contains(item()?['Text'], string(3))"
                },
                "runAfter": {
                    "Initialize_Reference_Array": [
                        "Succeeded"
                    ]
                },
                "type": "Query"
            },
            "Initialize_Found_Index": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Found Index",
                            "type": "integer",
                            "value": "@if(greater(length(body('Filter_array')), 0), first(body('Filter_array'))['Index'], -1)"
                        }
                    ]
                },
                "runAfter": {
                    "Filter_array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Reference_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Reference Array",
                            "type": "array",
                            "value": [
                                {
                                    "Index": 0,
                                    "Text": "This is a test 1"
                                },
                                {
                                    "Index": 1,
                                    "Text": "This is a test 2"
                                },
                                {
                                    "Index": 2,
                                    "Text": "This is a test 3"
                                },
                                {
                                    "Index": 3,
                                    "Text": "This is a test 4"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}
0
votes

Considering this to be my .txt file

enter image description here

In the beginning, I'm just retrieving the text file and converting each line into an array.

enter image description here

Then I have Initialised 2 Variables and set Index to 0 and Status to false.

enter image description here

In the next step I'm using Until Connector, looping through the Array using outputs('Convert_txt_into_array')[variables('Index')] and check until the status is set to true.

enter image description here

So, Whether the status becomes true or false the index increments by 1 and loops through the Until connector. If the condition becomes true then the Index Variable is set to the current iteration by using iterationIndexes which gives us the current iteration and exits the Until block.

And Finally, I'm printing Index Variable in Compose connector for future usage [This step can be avoided].

enter image description here

RESULT - 1 :

enter image description here

RESULT - 2 :

enter image description here

Codeview of my logic app

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": "The @{variables('FindingString')} is at index : @{variables('Index')}",
                "runAfter": {
                    "Until": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Convert_txt_into_array": {
                "inputs": "@split(body('Get_blob_content_(V2)'),'\r\n')",
                "runAfter": {
                    "FindingString": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "FindingString": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FindingString",
                            "type": "string",
                            "value": "age-34"
                        }
                    ]
                },
                "runAfter": {
                    "Get_blob_content_(V2)": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Get_blob_content_(V2)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureblob_1']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyJTJmc2FtcGxlVGV4dEZpbGUudHh0'))}/content",
                    "queries": {
                        "inferContentType": true
                    }
                },
                "metadata": {
                    "JTJmY29udGFpbmVyJTJmc2FtcGxlVGV4dEZpbGUudHh0": "/container/sampleTextFile.txt"
                },
                "runAfter": {},
                "type": "ApiConnection"
            },
            "Index": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Index",
                            "type": "integer",
                            "value": 0
                        }
                    ]
                },
                "runAfter": {
                    "Convert_txt_into_array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Status": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Status",
                            "type": "boolean",
                            "value": false
                        }
                    ]
                },
                "runAfter": {
                    "Index": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Until": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Set_Index": {
                                "inputs": {
                                    "name": "Index",
                                    "value": "@iterationIndexes('Until')"
                                },
                                "runAfter": {
                                    "Set_Status_as_True": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "SetVariable"
                            },
                            "Set_Status_as_True": {
                                "inputs": {
                                    "name": "Status",
                                    "value": "@true"
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "Set_Status_as_False": {
                                    "inputs": {
                                        "name": "Status",
                                        "value": "@false"
                                    },
                                    "runAfter": {},
                                    "type": "SetVariable"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@outputs('Convert_txt_into_array')[variables('Index')]",
                                        "@variables('FindingString')"
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "Index",
                            "value": 1
                        },
                        "runAfter": {
                            "Condition": [
                                "Succeeded"
                            ]
                        },
                        "type": "IncrementVariable"
                    }
                },
                "expression": "@equals(variables('Status'), true)",
                "limit": {
                    "count": 60,
                    "timeout": "PT1H"
                },
                "runAfter": {
                    "Status": [
                        "Succeeded"
                    ]
                },
                "type": "Until"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azureblob_1": {
                    "connectionId": "/subscriptions/<Your_Subscription_Id>/resourceGroups/<Your_Resource_Group_Name>/providers/Microsoft.Web/connections/azureblob-1",
                    "connectionName": "azureblob-1",
                    "id": "/subscriptions/<Your_Subscription_Id>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
                }
            }
        }
    }
}