Link to home
Start Free TrialLog in
Avatar of Rossamino
Rossamino

asked on

vbscript code for adding two 2-dimensional arrays

I need to add together some very large arrays in vbscript.  I've written some code with a couple of test arrays, but it ain't working and my head is hurting.  It sorta works, but not really.  Here's the code:
dim arr1(3,3)
dim arr2(2,3)

arr1(0,0) = "abcd"
arr1(0,1) = "bcde"
arr1(0,2) = "cdef"
arr1(1,0) = "defg"
arr1(1,1) = "efgh"
arr1(1,2) = "fghi"
arr1(2,0) = "ghij"
arr1(2,1) = "hijk"
arr1(2,2) = "ijkl"

arr2(0,0) = "zyxw"
arr2(0,1) = "yxwv"
arr2(0,2) = "xwvu"
arr2(1,0) = "wvut"
arr2(1,1) = "vuts"
arr2(1,2) = "utsr"

thingy = ArrayConcat(arr1,arr2)

for x = 0 to ubound(arr1)
	for y = 0 to ubound(arr2)
		msgbox x & " " & y & " " & thingy(x,y)
	next
next


Private Function ArrayConcat(arr1, arr2)
	if UBound(arr1,2) >= UBound(arr2,2) then
		Columnz = UBound(arr1,2)
	else
		Columnz = UBound(arr2,2)
	end if

    ReDim ret(UBound(arr1,1) + UBound(arr2,1) + 1, Columnz)
    For i = 0 To UBound(arr1,1)
		for j = 0 to Ubound(arr1,2)
			ret(i,j) = arr1(i,j)
		next
    Next
    offset = Ubound(arr1,1) + 1
    For i = 0 To UBound(arr2,1)
		For j = 0 To Ubound(arr2,2)
			ret(i + offset,j) = arr2(i,j)
		Next
    Next
    ArrayConcat = ret
End Function

Open in new window


It should come up with
      0           1         2
0  abcd    bcde    cdef
1  defg    efgh    fghi
2  ghij    hijk    ijkl
3  zyxw    yxwv    xwvu
4  wvut    vuts    utsr

But it isn't :-(
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rossamino
Rossamino

ASKER

Thanks for the help.