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
about 6 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
about 6 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
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 ,