Talend Connect
Virtual Summit
JOIN US!
And visit the Customer
& Community Lounge.
May 27-28, wherever you are.

Take comma separated string and input into list

Highlighted
Four Stars

Take comma separated string and input into list

Hi,
I have a string input as a context (myContext = "item1,item2,item3,item4") and I want to be able to iterate over the comma separated values. I have used a tNormalise component to separate out the elements of the string but can I load these into an array so that I can create an iterative loop and do something with each element?
Thanks,
Dan
Highlighted
Community Manager

Re: Take comma separated string and input into list

Yes you can. If you are receiving them in a context variable, you can use a tJavaFlex. Convert them to an ArrayList in the Start Code section and then open a While loop in that section. Use the Main Code section to do whatever you want to do with the ArrayList (maybe output a row for each value?) and then close the loop in the End Code section.
Although I am not doing precisely this in this tutorial (https://www.rilhia.com/quicktips/quick-tip-row-multiplication) it demonstrates how you can generate rows using a tJavaFlex. If you have some Java, you should be able to extrapolate.
Highlighted
Forteen Stars

Re: Take comma separated string and input into list

You can do as described above with tJavaFlex,
as alternative You can use 
tInputXXX -> tNormalise -> tFlowToIterate  and then continue work with single element in Loop
tInputXXX -> onSubJobOk - will continue Job after finish the loop
-----------
Highlighted
Four Stars

Re: Take comma separated string and input into list

Thanks both, vapoukov's method works a treat and avoids me using any java. Rilhia Solutions looks like a great resource - thanks.
Highlighted
Five Stars

Re: Take comma separated string and input into list

I have also come across this scenario, I have fixed my issue by using below flow

trowgenerator> tnormalize > tflowtoiterate > tjava

[Note: In tRowGenerator you have to get the context variables ,which you have in your configuration file.Then In tnormalize component  separated by comma ]

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog