Jump to content

Looking for an easy way to merge two arrays


Recommended Posts

I'de like to take two arrays and merge them into one.

These are the typical AutoIT arrays, where element 0 is the count so I'd like to take elements 1 - ubound(array) of both arrays and combine them. PHP has a function that does this but I'm hopinn for something like this:

_ArrayMerge($aArray1,$aArray2)

Where $aArray2 would get appended to $aArray1

Worst case, I could just write one....I just may :D

TIA

Edited by sshrum

Sean Shrum :: http://www.shrum.net

All my published AU3-based apps and utilities

'Make it idiot-proof, and someone will make a better idiot'

 

Link to comment
Share on other sites

you'll have to write your own

#include <Array.au3>
Dim $avArray[5]
Dim $avArray2[5]
$avArray[0] = "JPM"
$avArray[1] = "Holger"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Jeremy"
$avArray2[0] = "Valik"
$avArray2[1] = "Cyberslug"
$avArray2[2] = "Nutster"
$avArray2[3] = "JdeB"
$avArray2[4] = "Tylo"
_ArrayDisplay($avArray, "Whole array")
_ArrayDisplay($avArray2, "Whole Array 2")
_ArrayMerge($avArray, $avArray2)
_ArrayDisplay($avArray, "Updated Array")


Func _ArrayMerge(ByRef $a_base, ByRef $a_add, $i_start = 0)
    Local $x
    For $x = $i_start To UBound($a_add) - 1
        _ArrayAdd($a_base, $a_add[$x])
    Next
EndFunc  ;==>_ArrayMerge
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • 9 months later...

How about this:

Func _ArrayMerge (ByRef $array1, ByRef $array2 )
    _ArrayDelete ( $array1, 0 )
    _ArrayDelete ( $array2, 0 )
    ReDim $array1[UBound($array1) + UBound($array2) ]
    For $x = 0 To UBound($array2) - 1
        $array1[UBound($array1) - UBound($array2) + $x] = $array2[$x]
    Next
    ReDim $array1 [UBound($array1) + 1]
    For $x = UBound($array1) - 1 To 1 step -1
        $array1[$x] = $array1[$x - 1]
    Next
    $array1[0] = UBound($array1) - 1
    ;~ _ArrayDisplay($array1, "test")
    Return $array1
EndFunc    ;==>_ArrayMerge
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...