# Joining two 2D-Array with search

## Recommended Posts

Hi there!

I have two 2D-Arrays.

If the first two columns of the second one fit the data the first two colums in the first array, I want to add the data of the second one into that row.

```\$aFirst[0][0] = "Row 1 | Column 1"
\$aFirst[0][1] = "Row 1 | Column 2"
...
\$aFirst[9][0] = "Row 10 | Column 1"
\$aFirst[9][1] = "Row 10 | Column 2"

\$aSecond[0][0] = "Row 1 | Column 1"
\$aSecond[0][1] = "Row 1 | Column 2"
\$aSecond[0][2] = "data 1a"
\$aSecond[0][3] = "data 1b"
...
\$aSecond[9][0] = "Row 10 | Column 1"
\$aSecond[9][1] = "Row 10 | Column 2"
\$aSecond[9][3] = "data 10a"
\$aSecond[9][3] = "data 10b"```

After joining it is supposed to look like this:

```\$aNew[0][0] = "Row 1 | Column 1"
\$aNew[0][1] = "Row 1 | Column 2"
\$aNew[0][2] = "data 1a"
\$aNew[0][3] = "data 1b"
...
\$aNew[9][0] = "Row 10 | Column 1"
\$aNew[9][1] = "Row 10 | Column 2"
\$aNew[9][2] = "data 10a"
\$aNew[9][3] = "data 10b"```

I already have some ideas (using Loops and so on, but maybe there is an UDF that I'm missing).

The array dimenions will be around aFirst[500][8] and for aSecond[750][20].

Note that it won't be that \$aFirst[\$i][0] won't equal \$aSecond[\$i][0] in the script.

Thank for suggestions :-)

EDIT:

```#include

Dim \$aFirst[10][3], \$aSecond[54][6]

\$aFirst[0][0] = "Row 1 | Column 1"
\$aFirst[0][1] = "Row 1 | Column 2"
\$aFirst[9][0] = "Row 10 | Column 1"
\$aFirst[9][1] = "Row 10 | Column 2"
\$aFirst[9][2] = "wixxer"

\$aSecond[0][0] = "Row 1 | Column 1"
\$aSecond[0][1] = "Row 1 | Column 2"
\$aSecond[0][2] = "data 1a"
\$aSecond[0][3] = "data 1b"
\$aSecond[20][0] = "Row 10 | Column 1"
\$aSecond[20][1] = "Row 10 | Column 2"
\$aSecond[20][2] = "data 10a"
\$aSecond[20][3] = "data 10b"
\$aSecond[20][5] = "data asdasd"

\$iArrayDimDiffernce = UBound(\$aSecond, 2) - UBound(\$aFirst, 2)
ReDim \$aFirst[UBound(\$aFirst)][UBound(\$aSecond, 2)+1]

For \$i = 0 To UBound(\$aFirst, 1) - 1
\$aFirst_Column_1 = \$aFirst[\$i][0]
\$aFirst_Column_2 = \$aFirst[\$i][0]
For \$j = 0 To UBound(\$aSecond, 1) - 1
\$aSecond_Column_1 = \$aSecond[\$j][0]
\$aSecond_Column_2 = \$aSecond[\$j][1]
If \$aFirst_Column_1 <> "" And \$aFirst_Column_2 <> "" And \$aSecond_Column_1 <> "" And \$aSecond_Column_2 <> "" Then
If \$aFirst_Column_1 = \$aSecond_Column_1 And \$aFirst_Column_2 And \$aSecond_Column_2 Then
For \$k = 2 To UBound(\$aSecond, 2) - 1
ConsoleWrite(\$k & @TAB & UBound(\$aSecond, 2) - 1 & @CR)
\$aFirst[\$i][\$k - 2 + \$iArrayDimDiffernce] = \$aSecond[\$j][\$k]
Next
EndIf
EndIf
Next
Next```

That is doing the job, but maybe there is a faster way?

Edited by yetrael

##### Share on other sites

Using 2 nested loops like you are is probably the best way. Someone else on here may have a better idea but that's how I would accomplish it.

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...