How to use For Each loop and add data row with a splitted string

Hello,

In my assignment, I am supposed to "translate a string ‘7 3 6 4 10 5 2 8 1 9’ into a datatable in 10 rows so that each row contains one number. As a tip it is suggested using split+for each+add row.

How should I be able to utilize the splitted string in the add data row activity correctly? I haven’t been able to add any rows in the table.

Thank you in advance!

Hi @mainer

Try with Generate Data Table From Text activity

Output:

Regards,

@mainer,

Iterate the output variable of Split Text activity. It would be an array.

In For each, Add Data Row instead of {7,3,6,..} pass below code

New Object() {numbersArray}

Sample Solution:

  1. Build Data Table to initialize data table to store the values.

  2. Split the String

  1. For Each to iterate and add to datatable.

Thanks,
Ashok :slight_smile:

Hi @mainer

Output

Hope it helps!!

When string as Column Datarow is needed:
grafik

When Strings are later used as int we can also handle

@ashokkarale

Hey,

I couldn’t successfully iterate the output variable of Split Text activity to be an array. Is the type Array of [T]? I am not sure whether it makes a difference since I’m using the activity ‘For Each Row in Datatable’

Many thanks!

@mainer,

Create variable from Split text output property All extracted text. Press ctrl+k to create variable.

Otherwise change the arrString variable datatype to this one:

Thanks,
Ashok :slight_smile:

@ashokkarale After creating such type variable, what is the compatible type as List of items in For Each Row?

@mainer

Argument Type of For Each should be String.

Thanks,
Ashok :slight_smile:

@mainer

kindly note that with above given activity

it can be done in one go, had you tried?

thank you @ppr, I’ll have a look on this next since even with compatible types of variables, the compilation of activities fails and the following errors arise
image

Hi @mainer

Try the below flow.

Please find below workflow for your reference
Sequence22.xaml (13.7 KB)

Output

MicrosoftTeams-image6

Regards,
Chandrika.

@Sai_Chandrika_Kopparapu Hello,

I am unable to output the data to the excel file I created for this, at least it remains blank

You don’t use For Each Row in Data Table. You don’t have a datatable. You have an array.
Use a regular For Each.

Also, you have numbersArray in the For Each’s Item property. This indicates you don’t quite understand the For Each.

Your array is myData - ie an array with the elements 7, 3, 6, 4 etc

When you do a For Each it loops through those items. On the first iteration of the loop the value of the Item is 7. On the second iteration it’s 3. On the third iteration it’s 6. etc.

Switch to a regular For Each, change “numbersArray” to currentNumber and it’ll make more sense. Then you just put {currentNumber} into the ArrayRow property of the Add Data Row.

Also, you don’t need to use the Split Text activity. Store “7 3 6 4…” etc in a string variable. Then in the For Each put yourStringVarName.Split(" "c) which does the same thing as the Split Text activity.

1 Like