How to fill datatable with 3 differently lengthed arrays

hello ,
i have built a new data table with 3 columns of string data type
i have 3 arrays of different lengths i want to place in each column
lets say a_array={“123”,“123”)
b_array={“456”,“456”,“456”)
c_array={“789”,“789”,“789”,“789”}

how do i fill the datatable such that even if the arrays’ lengths change , the data is placed in each column correctly with no out of bounds exceptions
i use vba not c#
please help me with this , thanks :slight_smile:

Hi.

It will depend on what you are using for your Column Names.

Let’s say you have a Build Data Table activity to generate a datatable with 1 column named “Column-1”

Then, you will need to add a column for each one that doesn’t exist in the datatable.

ForEach activity:
   For each item in a_array // use Output property for index
       IF activity: condition: Not dt1.Columns.Contains("Column-"+index.ToString)
             Add Data Column // "Column-"+index.ToString
 Add Data Row activity // a_array

This is just psuedocode which you would implement in UiPath Studio.

Hope that helps.

Regards.

thank you for replying ,
i tried to do this but i get an error in the IF condition activity
"System.Data.index’ is not accessible in this context because it is ‘Friend’.

and i think you may have missunderstood , i know the exact column names , lets call them Col1 Col2 Col3 , its the contents of the arrays that change not the Z in column-Z
for example :
from this built table , to look like that (after filling it with the 3 different arrays)


hope this clarifies
thanks

Yep, I misunderstood.

First, you need to know which array has the most items, so you can loop that number of times.

Since you know there are 3 arrays or 3 columns, we can just store the upper bounds of each one into an array, and take the largest value.

Assign: numberOfRows = {UBound(a_array),UBound(b_array),UBound(c_array)}.Max

Then, loop from 0 to that number. We can use enumerable.range() to create that.
For each rowIndex In Enumerable.Range(0,numberOfRows+1)

Finally, you just need to make sure the index exists in the array with an inline IF when you assign it to a new array that represents the row. And, use Add Data Row with that new array.
Assign activity: newArr = { If(UBound(a_array)>=rowIndex,a_array(rowIndex),""), If(UBound(a_array)>=rowIndex,b_array(rowIndex),""), If(UBound(a_array)>=rowIndex,c_array(rowIndex),"") }

Add Data Row // use newArr in ArrayRow property

In the end it would look like this:

Assign: numberOfRows = {UBound(a_array),UBound(b_array),UBound(c_array)}.Max
For each rowIndex In Enumerable.Range(0,numberOfRows+1)
    Assign activity: newArr = { If(UBound(a_array)>=rowIndex,a_array(rowIndex),""), If(UBound(a_array)>=rowIndex,b_array(rowIndex),""), If(UBound(a_array)>=rowIndex,c_array(rowIndex),"") }```
    Add Data Row // use newArr in ArrayRow property

Hopefully, that’s more in line with what you are trying to do.

Regards