Jump to content
Sign in to follow this  
whitts

Merge arrays

Recommended Posts

whitts

Ok say i have 3 arrays

$array1

$array2

$array3

How could i add $array2 to the end of array $array1 and then add $array3 to the end of $array1 (which would at this point would contain $array2)

Thanks

Share this post


Link to post
Share on other sites
weaponx

#include<array.au3>

Dim $array1[4] = [0,1,2,3]
Dim $array2[4] = [4,5,6,7]
Dim $array3[4] = [8,9,10,11]

$array4 = ArrayMerge($array1, $array2)
$array4 = ArrayMerge($array4, $array3)

_ArrayDisplay($array4)

;$amArray1: Array to add to
;$amArray2: Array to merge into previous
Func ArrayMerge($amArray1, $amArray2)
    Local $tempArray = $amArray1
    Local $amElements = Ubound($tempArray) + Ubound($amArray2)
    Redim $tempArray[$amElements]
    Local $amCount = 0
    For $X = Ubound($amArray1) to $amElements - 1
        $tempArray[$X] = $amArray2[$amCount]
        $amCount += 1
    Next
    Return $tempArray
EndFunc

Share this post


Link to post
Share on other sites
whitts

Thanks weaponx, that code works a treat

Share this post


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
Sign in to follow this  

×