How to split string into a list of elements that I can iterate through?
I have a string of text that I need to split into an array. I then need to loop through each element of the array and perform an action. How would I go about splitting the text, then iterate through each element of the array?
I've attempted to use the split function,
[PILL].split(" ")
but that doesn't yield an array that I can iterate through.
Best Answer
A
Amanda Wong
said
over 7 years ago
Example
You have a string of priority levels 'high medium high low low high'. You want transform the string into a "list data pill" that you can use the Repeat action on.
Part 1: Define the size of the list
You can use Utilities (app) to Create list (action) with a size corresponding to the length of your string (assume contained in "Message" data pill).
This means if you have a string 'high medium high low low high', you will create a list of size 6 - let's call this 'List A'.
This action is useful when you want to use a Repeat Action to repeat a step for a specific number of times.
Part 2: Create the actual list
After 'List A' has been created, add a new step Repeat Action
For each item in 'List A', use List (app), Append item to a new list (action) - let's name this 'Test'.
Set up the list item field in formula mode as follows
This tells Workato to repeat this step 6 times, to pick up words in the string at Index=0, Index=1, Index=2, ... , Index=5 during each step to append into a new list.
This results in an array of 6 elements being created - let's call this 'List B', effectively transforming words in a string into elements in an array.
Now, you have created a list containing 6 elements ["high", "medium", "high", "low", "low", "high"] that you can iterate through using a Repeat Action.
For example, you can loop through each of the priority levels and post a message to an app. This will create 6 distinct messages in the app.
Note:
.length returns the number of elements in an array
.length returns the number of words in a string with more than one word
.length returns the number of characters in a string with only one word
List (app) allows you to store information in a list format by creating a new list that you can add items to within the same step.
Detailed screenshots of recipe steps:
Part 1: Define the size of the list
Part 2: Create the actual list
Answer
Amanda Wong
said
over 7 years ago
Example
You have a string of priority levels 'high medium high low low high'. You want transform the string into a "list data pill" that you can use the Repeat action on.
Part 1: Define the size of the list
You can use Utilities (app) to Create list (action) with a size corresponding to the length of your string (assume contained in "Message" data pill).
This means if you have a string 'high medium high low low high', you will create a list of size 6 - let's call this 'List A'.
This action is useful when you want to use a Repeat Action to repeat a step for a specific number of times.
Part 2: Create the actual list
After 'List A' has been created, add a new step Repeat Action
For each item in 'List A', use List (app), Append item to a new list (action) - let's name this 'Test'.
Set up the list item field in formula mode as follows
This tells Workato to repeat this step 6 times, to pick up words in the string at Index=0, Index=1, Index=2, ... , Index=5 during each step to append into a new list.
This results in an array of 6 elements being created - let's call this 'List B', effectively transforming words in a string into elements in an array.
Now, you have created a list containing 6 elements ["high", "medium", "high", "low", "low", "high"] that you can iterate through using a Repeat Action.
For example, you can loop through each of the priority levels and post a message to an app. This will create 6 distinct messages in the app.
Note:
.length returns the number of elements in an array
.length returns the number of words in a string with more than one word
.length returns the number of characters in a string with only one word
List (app) allows you to store information in a list format by creating a new list that you can add items to within the same step.
Detailed screenshots of recipe steps:
Part 1: Define the size of the list
Part 2: Create the actual list
S
Saul Macht
said
over 7 years ago
What if I have a string "a,b,c,d,e"? How do I get the number of characters (equal 5)?
Amanda Wong
said
over 7 years ago
Use .split(",").length to return no. of characters separated by ,
J
Jmcvicker
said
8 months ago
There is a faster way. I needed to create an iterable list from a comma-delimited string.
App: Lists by Workato Action: Accumulate items to list
Setup: give list a name and a list item field name, then just take your comma-delimited string and split it
L
Lachlan Macphee
said
4 months ago
The answer above creates a field id which is an array of strings for me, rather than the string itself. So I believe you'd have to double iterate, not ideal...
The way I found to do this was to create a variable using the "Variables by Workato" app. I then had to edit the variable of this schema so that it was an array of strings, like so:
[
{
"name": "ids",
"type": "array",
"of": "string",
"label": "ids",
"optional": true,
"details": {
"real_name": "ids"
}
}
]
then I was able to set the initial value:
then in the next step you can iterate through this list.
This is way harder than it should be, and I wish you could just create a list easily from a string split by a delimiter.
Saul Macht
How to split string into a list of elements that I can iterate through?
I have a string of text that I need to split into an array. I then need to loop through each element of the array and perform an action. How would I go about splitting the text, then iterate through each element of the array?
I've attempted to use the split function,
[PILL].split(" ")
but that doesn't yield an array that I can iterate through.
Example
You have a string of priority levels 'high medium high low low high'. You want transform the string into a "list data pill" that you can use the Repeat action on.
Part 1: Define the size of the list
Part 2: Create the actual list
Note:
Part 2: Create the actual list
Amanda Wong
Example
You have a string of priority levels 'high medium high low low high'. You want transform the string into a "list data pill" that you can use the Repeat action on.
Part 1: Define the size of the list
Part 2: Create the actual list
Note:
Part 2: Create the actual list
Saul Macht
What if I have a string "a,b,c,d,e"? How do I get the number of characters (equal 5)?
Amanda Wong
Use .split(",").length to return no. of characters separated by ,
Jmcvicker
There is a faster way. I needed to create an iterable list from a comma-delimited string.
App: Lists by Workato
Action: Accumulate items to list
Setup: give list a name and a list item field name, then just take your comma-delimited string and split it
Lachlan Macphee
The answer above creates a field id which is an array of strings for me, rather than the string itself. So I believe you'd have to double iterate, not ideal...
The way I found to do this was to create a variable using the "Variables by Workato" app. I then had to edit the variable of this schema so that it was an array of strings, like so:
[
{
"name": "ids",
"type": "array",
"of": "string",
"label": "ids",
"optional": true,
"details": {
"real_name": "ids"
}
}
]
then I was able to set the initial value:
then in the next step you can iterate through this list.
This is way harder than it should be, and I wish you could just create a list easily from a string split by a delimiter.
1 person likes this
Jordan. molina
Above solution is the best solution