Iterating over an array an setting variables conditionally

Hi,

I want to set two variables by iterating over a collection of elements from the Slack message.
Variables to be set:

  1. url
  2. text

Here is the code that I want to reflect in make:

for element in elements:
    if element["type"] == "link":
        url = element["url"]
    elif element["type"] == "text":
        text = element["text"]

How can I achieve this in make? I tried using the ‘Set multiple variables’ module but it returns values only from the first element.

Something like this perhaps?

Screenshot_2024-04-26_220444

Output

Screenshot_2024-04-26_220454

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 88,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "url",
                                "value": "{{first(map(83.blocks; \"elements.1.elements.1.url\"))}}"
                            },
                            {
                                "name": "type",
                                "value": "{{first(map(83.blocks; \"elements.1.elements.1.type\"))}}"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": -688,
                            "y": -3125
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        null,
                                        null
                                    ]
                                },
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "type": "array",
                                "label": "Variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "label": "Variable name",
                                        "type": "text",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "label": "Variable value",
                                        "type": "any"
                                    }
                                ]
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "url",
                                "label": "url",
                                "type": "any"
                            },
                            {
                                "name": "type",
                                "label": "type",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
2 Likes

Thank you, I also managed to solve it in a slightly different way:

However, I think your solution is more elegant. Just need to digest the syntax you provided :slight_smile:

1 Like

Basically I select the top-most level array, and “drill” down (by the 1 in the selector).

In your case, you are selecting the lowest-level array, and Make assumes the first item of each child array on the way down.

Then, get with the index 1 is the same as just using the first function.

3 Likes