Adding values to datatable from two different arrays

Hii Can anyone help me to add values to a datatable from two different arrays where first column values are present in first string array and corresponding second column values are present in second int array. If there are repeating columns in first column then corresponding int array values should be added so that all columns are unique.
Thanks in advance

1 Like

Hi @Gattu_Monika let’s say you already built datatable dtDataTable with columns Column1 and Column2 and you have string arrays arrString1 and arrString2. All you have to do is do the following:

For Each item in arrString1
Add datarow to dtDataTable
If intIndex = 0
     Assign dtDataTable(intIndex)("Column1") = item
     Assign intIndex = intIndex + 1
Else
     LookUp dtDatatable, LookUp Value = item, CellValue = strCellValue
     If strCellValue = "" Then
     Assign dtDataTable(intIndex)("Column1") = item
     Assign intIndex = intIndex + 1

Create another For Each for arrString2 and replace dtDataTable(intIndex)(“Column1”) with dtDataTable(intIndex)(“Column2”) and this time you don’t need to add datarow as datarows has been already created by the previous For Each, unless you expect to have an unequal number of unique items in your two arrays

2 Likes

Thanks @avejr748
But if there are repeating columns then corresponding second column int elements should be added…can you tell me how to add it

Hello @Gattu_Monika, what do you mean repeating columns? I thought you only have 2 columns in your use case.

i have repeating values in column 1 and if they are repeating then corresponding values in second column are added and duplicates are removed in first column

Oh, I see… I misinterpreted your requirements on your first message. My bad…
Let me clarify some things:

  1. Your datatable is empty from the start or scraped/read from some data source?

  2. Your string array and int array are data sources or you just intend to use it programmatically to manipulate/work with your datatable?

  3. My interpretation now is that you have a datasource and put into datatable, now you would like to work on the datatable to remove duplicates from column1 and just provide the number of times that it has appeared in column2. Is this what you need?

@avejr748 i think i explained it wrong…i have two arrays like first string array contains some names and second array contains corresponding data of first string array …
arr1={apple,mango,banana,papaya,apple,mango}
arr2={2,3,8,5,7,3}
i need to convert above two arrays in to datatable containing column 1 and 2
where col1 should contain first array and if elements repeat then corresponding values in second array must be ADDED.
required output
column1 column2
apple 9
mango 6
banana 8
Papaya 5
apple and mango are repeated so corresponding values of second array must be added

@avejr748 Sorry for explaining wrong.
Thanks in advance

Hi @Gattu_Monika no worries, totally understandable that it’s hard to explain requirements. By the way, it would be a bit complex now to describe the solution so I made a sample workflow for you. I got your desired output based on your sample. Feel free to modify further to get your final desired outcome.

TwoArraysToDataTable.xaml (21.1 KB)

@avejr748 thanks i will try it…

@Gattu_Monika I have included annotations in the workflow. If the workflow is as you expect, kindly mark the answer as a solution for others to easily find it. Thanks

1 Like

@avejr748 Thankyou so much…It worked very well

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.