Jump to content

2D Array - How do we add values to the beginning of new columns ?


Recommended Posts

Hello,

 

Let's say that we have an $array[0][2] which has values on the first column.

Could you please tell me how i could add values in the second column of the array, but starting from position 0 - $array[0][2] ?

This method should work for any amount of inserted values, i should not insert them manually.

 

I'm currently using the following method, which i don't consider to be elegant; Is there a better / quicker / shorter way to do it ?

#Include <Array.au3>


Local $BaseArray[0][2]

Local $FillArray[2][2] = [["New Item 2 - 1", "New Item 2 - 2"],["New Item 3 - 1", "New Item 3 - 2"]]

_ArrayAdd($BaseArray,$FillArray)
_ArrayDisplay($BaseArray)


Local $FillArray2[3][2] = [["New Item 4 - 1", "New Item 4 - 2"],["New Item 5 - 1", "New Item 5 - 2"],["New Item 6 - 1", "New Item 6 - 2"]]

$length = UBound($BaseArray,1) > UBound($FillArray2,1) ? UBound($BaseArray,1) : UBound($FillArray2,1)
ReDim $BaseArray[$length][UBound($BaseArray,2)+UBound($FillArray2,2)]

for $j = 0 to UBound($FillArray2,2)-1
    for $i = 0 to UBound($FillArray2,1)-1
        $BaseArray[$i][UBound($BaseArray,2)-2+$j] = $FillArray2[$i][$j]
    next
Next

_ArrayDisplay($BaseArray)

Thank you.

Edited by mexykanu
Link to comment
Share on other sites

Hello. Create your own function like this.

 

#include <Array.au3>


Local $BaseArray[0][2]

Local $FillArray[2][2] = [["New Item 2 - 1", "New Item 2 - 2"], ["New Item 3 - 1", "New Item 3 - 2"]]

_ArrayAdd($BaseArray, $FillArray)
_ArrayDisplay($BaseArray)

Local $FillArray2[2][2] = [["New Item 4 - 1", "New Item 4 - 2"], ["New Item 5 - 1", "New Item 5 - 2"]]

_ArrayCombine($BaseArray, $FillArray2)

_ArrayDisplay($BaseArray)


Func _ArrayCombine(ByRef $array1, ByRef $array2) ; Function to combine 2D arrays in column direction
    Local $a1 = UBound($array1, 2)
    Local $a2 = UBound($array2, 1)
    ReDim $array1[UBound($array1, 1)][UBound($array1, 2) + UBound($array2, 2)]
    For $i = 0 To $a2 - 1
        For $x = $a1 To $a1 + UBound($array2, 2) - 1
            $y = $x - $a1
            $array1[$i][$x] = $array2[$i][$y]
        Next
    Next
EndFunc   ;==>_ArrayCombine

Saludos

Link to comment
Share on other sites

1 hour ago, Danyfirex said:

Hello. Create your own function like this.

 

#include <Array.au3>


Local $BaseArray[0][2]

Local $FillArray[2][2] = [["New Item 2 - 1", "New Item 2 - 2"], ["New Item 3 - 1", "New Item 3 - 2"]]

_ArrayAdd($BaseArray, $FillArray)
_ArrayDisplay($BaseArray)

Local $FillArray2[2][2] = [["New Item 4 - 1", "New Item 4 - 2"], ["New Item 5 - 1", "New Item 5 - 2"]]

_ArrayCombine($BaseArray, $FillArray2)

_ArrayDisplay($BaseArray)


Func _ArrayCombine(ByRef $array1, ByRef $array2) ; Function to combine 2D arrays in column direction
    Local $a1 = UBound($array1, 2)
    Local $a2 = UBound($array2, 1)
    ReDim $array1[UBound($array1, 1)][UBound($array1, 2) + UBound($array2, 2)]
    For $i = 0 To $a2 - 1
        For $x = $a1 To $a1 + UBound($array2, 2) - 1
            $y = $x - $a1
            $array1[$i][$x] = $array2[$i][$y]
        Next
    Next
EndFunc   ;==>_ArrayCombine

Saludos

Hello,

 

Thank you, now i've also learned the importance of ByRef.

 

Best Regards,

Daniel.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...