Recommended Posts

Before I start code my own function to do that I am wondering if there is a ready function or UDF to add 2d array to another 2d array but to the "right" instead of to the "bottom" ?

For example I have two 2d array which looks this way:

first array:

a1 | a2 | a3
b1 | b2 | b3

second array:
a4 | a5 | a6
b4 | b5 | b6

both arrays has 3 colums and two rows

I want combine this two arrays to get 2d array with 6 columns and two rows:

a1 | a2 | a3 | a4 | a5 | a6
b1 | b2 | b3 | b4 | b5 | b6

I have very big arrays with multiple columns and I want combine it toghether this way.

Anyone have any idea how to do it?

Edit: Ok nvm, I think I got it:

```Func ArrayAddRight(\$Array1, \$Array2)
Local \$aResult = \$Array1
Local \$rows1 = Ubound(\$aResult)
Local \$rows2 = Ubound(\$Array2)
Local \$cols1 = Ubound(\$aResult,2)
Local \$cols2 = Ubound(\$Array2,2)
Local \$cols = \$cols1+\$cols2
Local \$rows = \$rows1
if \$rows2 > \$rows1 then \$rows = \$rows2

ReDim \$aResult[\$rows][\$cols]
Local \$counter
for \$i=0 to \$rows - 1
\$counter = 0
for \$j=\$cols1 to \$cols - 1
\$aResult[\$i][\$j] = \$Array2[\$i][\$counter]
\$counter += 1
Next
Next
Return \$aResult
EndFunc```

Edited by maniootek
Share on other sites

@mandriospo You should pass your arrays with ByRef, especially if the arrays get larger.  Otherwise the arrays are copied and can consume quite some time for nothing.

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

Share on other sites

Now to run this you need ArrayColInsert (version 2)

```#include <Array.au3>
#include "ArrayInsertCol.au3"

;__ExampleA()
__ExampleB()
Func __ExampleA()
Local \$A_Array[][] = [['A1', 'A2', 'A3'], _
['B1', 'B2', 'B3'], _
['C1', 'C2', 'C3'], _
['D1', 'D2', 'D3'], _
['E1', 'E2', 'E3']]

Local \$B_Array[][] = [['A4', 'A5', 'A6'], _
['B4', 'B5', 'B6'], _
['C4', 'C5', 'C6'], _
['D4', 'D5', 'D6'], _
['E4', 'E5', 'E6']]

;~ insert at end
__ArrayColInsert_V2(\$A_Array, UBound(\$A_Array, 2), \$B_Array)
_ArrayDisplay(\$A_Array, '\$A_Array')
EndFunc

Func __ExampleB()
Local \$A_Array[][] = [['A1', 'A2', 'A3'], _
['B1', 'B2', 'B3'], _
['C1', 'C2', 'C3'], _
['D1', 'D2', 'D3'], _
['E1', 'E2', 'E3']]

Local \$B_Array[][] = [['A4', 'A5', 'A6'], _
['B4', 'B5', 'B6'], _
['C4', 'C5', 'C6'], _
['D4', 'D5', 'D6'], _
['E4', 'E5', 'E6']]

;~ insert from column 0
__ArrayColInsert_V2(\$A_Array, 0, \$B_Array)
_ArrayDisplay(\$A_Array, '\$A_Array')
EndFunc```

Create an account

Register a new account