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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RossaminoAuthor Commented:
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.