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 :-(
RossaminoAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Hi, try this.  I think the main problem is you were using the incorrect UBound value based on the zero based array.  And also in the your "thingy" output, you were outputting based on the UBound of arr1 and arr2, and not "thingy".

Regards,

Rob.

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(thingy, 1)
	for y = 0 to ubound(thingy, 2)
		WScript.Echo x & " " & y & " " & thingy(x,y)
	next
next


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

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

Open in new window

0
 
RossaminoAuthor Commented:
Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.