🔎 Variable Replace All: Replace a string array with another string array

Problem Statement :mag_right:

Do you have an array of strings that you want to replace with another array of strings?

The issue is that you can’t manually “chain” the replace built-in function, or Text Parser “Replace Text” modules without knowing the number of items in the array in advance.

Scenario

You can do this in about five modules:

The trick here is:

  • the “Get Variable” so you can get the updated text from the previous iteration of the repeater module, and
  • the “Text Aggregator” is set to return only the last/final replace.

1. Create search and replace arrays

In this example, we want to replace “test” with “SUCCESS”, and “hello” with “GOODBYE”:

Screenshot_2024-06-12_130628

Arrays are created by splitting by pipe character, you can use any delimiter of your choice.

2. Repeat by array length

This throws an error if the two arrays are not equal length.

3. Get variable of text

Screenshot_2024-06-12_120650

Get the current text variable, whether it’s from the initial module, or from the previous iteration of the repeater loop.

4. Perform replace

This demonstrates the use of the replace function. You could probably use a Replace module too. The variable name has to be the same as the initial module and the Get Variable module.

5. Aggregate to text, but only “Save” the final result

Instead of aggregating to an array, and then using the last function to get the last result, this trick saves you an operation.

Output, single bundle

samliew – request private consultation

Join the Make Fans Discord server to chat with other makers!

3 Likes

Hi @samliew

Great solution. However, we just wanted to know why you have gone with multiple modules instead of 2 or 3. This will help us understand the importance of your logic.

See the same solution implemented in 3 operations. We can again cut this to 2 operations.
Please check the images below.

Module One [Set Variable ] :-

Second Module [Iterater ] :-

Third Module [ Text Aggregator ] :-

Final Output : -

If you require additional assistance, please don’t hesitate to reach out to us.
MSquare Support
| Book Live Implementation
Visit us here
Youtube Channel

1 Like

This is incorrect. You’re not actually doing a replace.

What if the input text was this, and you want to replace ALL the “test” with “SUCCESS”, and ALL the “hello” with “GOODBYE”?

Screenshot_2024-06-12_130641

Does your scenario do this?

samliew – request private consultation

Join the Make Fans Discord server to chat with makers!

1 Like

Here, feel free to optimise this further; I’ll provide the module export below

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.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 1,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "text",
                                "value": "Lorem ipsum test sit amet, hello adipiscing elit, sed do eiusmod tempor incididunt ut labore et test magna aliqua. Ut enim ad minim hello, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea test hello. Duis aute irure test in reprehenderit in voluptate velit esse cillum test hello fugiat nulla pariatur. Excepteur sint occaecat cupidatat test proident, sunt in culpa qui officia deserunt hello anim id est hello."
                            },
                            {
                                "name": "searchArray",
                                "value": "{{split(\"test|hello\"; \"|\")}}"
                            },
                            {
                                "name": "replaceArray",
                                "value": "{{split(\"SUCCESS|GOODBYE\"; \"|\")}}"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": -560,
                            "y": -128,
                            "name": "Set Text, Search, Replace vars"
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        null,
                                        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": "text",
                                "label": "text",
                                "type": "any"
                            },
                            {
                                "name": "searchArray",
                                "label": "searchArray",
                                "type": "any"
                            },
                            {
                                "name": "replaceArray",
                                "label": "replaceArray",
                                "type": "any"
                            }
                        ]
                    }
                },
                {
                    "id": 2,
                    "module": "builtin:BasicRepeater",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "step": "1",
                        "start": "1",
                        "repeats": "{{if(length(1.searchArray) = length(1.replaceArray); length(1.searchArray); null)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -336,
                            "y": -129,
                            "name": "Replace ALL"
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "start",
                                "type": "number",
                                "label": "Initial value",
                                "required": true
                            },
                            {
                                "name": "repeats",
                                "type": "number",
                                "label": "Repeats",
                                "required": true,
                                "validate": {
                                    "max": 10000,
                                    "min": 0
                                }
                            },
                            {
                                "name": "step",
                                "type": "number",
                                "label": "Step",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 3,
                    "module": "util:GetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "text"
                    },
                    "metadata": {
                        "designer": {
                            "x": -114,
                            "y": -128,
                            "name": "Get current replaced text"
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            }
                        ],
                        "interface": [
                            {
                                "name": "text",
                                "type": "any",
                                "label": "text"
                            }
                        ]
                    }
                },
                {
                    "id": 4,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "text",
                        "scope": "roundtrip",
                        "value": "{{replace(3.text; 1.searchArray[2.i]; 1.replaceArray[2.i])}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 109,
                            "y": -129,
                            "name": "Perform replace"
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "text",
                                "type": "any",
                                "label": "text"
                            }
                        ]
                    }
                },
                {
                    "id": 5,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "feeder": 2,
                        "rowSeparator": "other",
                        "otherRowSeparator": ""
                    },
                    "mapper": {
                        "value": "{{if(2.i != length(1.searchArray); emptystring; 4.text)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 329,
                            "y": -128,
                            "name": "Get final replace result",
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "extra": {
                                "feeder": {
                                    "label": "Replace ALL"
                                }
                            },
                            "flags": {
                                "groupBy": {
                                    "collapsed": true
                                },
                                "stopIfEmpty": {
                                    "collapsed": true
                                }
                            },
                            "parameters": {
                                "rowSeparator": {
                                    "label": "Other"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            },
                            {
                                "name": "otherRowSeparator",
                                "type": "text",
                                "label": "Separator"
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

samliew – request private consultation

Join the Make Fans Discord server to chat with makers!

Hi @samliew ,

Thanks for your feedback. Now, we understand the requirement in detail and we can see you have updated the post with different images to avoid misunderstanding.

In this case, We have to update the scenario a little bit.
We have added “set and get variables” in between the iterator and text aggregator.


Also added one more condition in the filter after the set variable as shown in the below image.

Input :-

Output :-

In that case, you are copying me, but worse.

Because you don’t need to use ifempty and get functions.

And also you waste one module to match the final bundle.

samliew – request private consultation

Join the unofficial Make Discord server to chat with us!

Hi @samliew ,

Thanks for your comments. But not sure why you are saying map function as we are not using it and wasting any operation using an extra module.

Please test our workflow in your account. To do so:

  1. Download the attached blueprint and export to your make account by clicking the export button as shown below
    image

  2. Run the scenario and see the result. It won’t throw an error even if the length of arrays doesn’t match.

I don’t see any blueprint, did you forget to attach it?

samliew – request private consultation

Join the Make Fans Discord server to chat with other makers!

@samliew ,

Attaching the Blueprint here.
blueprint (3).json (10.0 KB)