Jump to content

Array add multiple columns with data (Array right add)


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
Link to post
Share on other sites
Link to post
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

 

Edited by jugador
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...