Jump to content
Sign in to follow this  


Recommended Posts



I just whipped up a script to merge any two 2-dimensional arrays. There are plenty of scripts out there to merge just 1-dimensional arrays and I felt I need 2-dimensional ones often enough to make a generic _MergeArray2() function. This function supports arrays of multiple bases, but the returned array is always a base-1 array with the element [0][0] being the size of the array.

; ----------------------------------------------------------------------------
; AutoIt Version: 3.1.0
; Author:        Jussi Pakkanen  email: vilufin@fastmail.fm
; Script Function:
;   Merges any two 2-dimensional arrays starting from [$base][0]. The arrays must be of same base, ie. 
; you cannot merge a base-0 array with a base-1 array.
; The returned array is always base-1, eg. element $mergedArray[0][0] = UBound($mergedArray) - 1
; and the values start from [1][0]
; Usage:
; $mergedArray = _MergeArray2($array1, $array2, $base)
; --------------------------------------------------------------------------------------------

Func _MergeArray2($array1, $array2, $base)
  ; Check if both arrays have 2 dimensions...
   If (UBound($array1, 0) <> 2) OR (UBound($array2, 0) <> 2) Then
      SetError(1); ...if not, set error state and return
  ;Adjust the size of $mergedArray to fit the contents of the arrays to be merged
  ;Check which array's dim2 is bigger and choose it as the dim2 size for $mergedArray
   If UBound($array1,2) > UBound($array2,2) Then 
      $dim2size = UBound($array1,2) - 1
      $dim2size = UBound($array2,2) - 1
  ;The size of the first dimension is both arrays' dim1 sizes combined
   $dim1size = (UBound($array1,1) - $base) + (UBound($array2,1) - $base)
   Dim $mergedArray[$dim1size + 1][$dim2size + 1]
  ;Enter the number of lines in the array into element [0][0]
   $mergedArray[0][0] = UBound($mergedArray) - 1
  ;Copy $array1 to the beginning of $mergedArray
   For $i = 0 to UBound($array1,1) - 1 - $base
      For $k = 0 to UBound($array1,2) - 1
         $mergedArray[$i+1][$k] = $array1[$i+$base][$k]

  ;Append $mergedArray with the contents of $array2
   For $i = 0 to UBound($array2,1) - 1 - $base
      For $k = 0 to UBound($array2,2) - 1
         $mergedArray[$i + 1 - $base + UBound($array1,1)][$k] = $array2[$i+$base][$k]
  ;Return the merged array
   Return $mergedArray

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.