• Documentation
  • Workato Blog
  • Product Blog
  • Return to Workato
  • Login
  • Automation Institute
  • Documentation
  • Community Forums
New Topic
Discussions Recipe Building Formula Mode Questions
S

Saul Macht

started a topic almost 6 years ago
Answered

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 almost 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). 
    image
  • 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
    image
  • 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
  • What is formula mode?
  • Learn more about formula mode here
  • Learn more about Create List (action) here
  • 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 almost 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). 
    image
  • 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
    image
  • 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
  • What is formula mode?
  • Learn more about formula mode here
  • Learn more about Create List (action) here
  • 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 almost 6 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 almost 6 years ago

Use .split(",").length to return no. of characters separated by ,

Login to post a comment

Still can't find your solution?

Send us a ticket, we will try our best to assist you with your problem

Documentation
Developer's Library
Tutorials
eBooks
Product Hour
Product Blog
Workato Blog
Product updates
Customer Stories
© Workato 2022   Privacy   Terms   +1 (844) 469-6752
  • Documentation
  • Workato Blog
  • Product Blog
  • Solutions
  • Forums
  • Tickets
  • Log in
  • Return to Workato
Topic views count