vbScript glue 3 arrays into one

Hi guys.

I have 3 arrays

MyArray1(200)
MyArray2(100)
MyArray3(50)

I am looking for a simple easy vbScript line to glue/join these 3 arrays together so I make a single array:

MySingleArray(350)   '    200 + 100 + 50 = 350 Elements

Any Ideas?

Thanks in advance
LVL 1
New_AlexAsked:
Who is Participating?
 
Bill PrewCommented:
Try this:

Dim AllArray()
AllArray = Split(Join(MyArray1,"|") & "|" & Join(MyArray2,"|") & "|" & Join(MyArray3,"|") ,"|")

Open in new window

~bp
0
 
StephenJRCommented:
I think you probably have to create a new array and loop through each one adding to the new one. Are your arrays using base 1? Otherwise MyArray1(200) has 201 elements.
0
 
borgunitCommented:
You can redim an array to the size of each arrays bounds and then populate it.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ScriptAddictCommented:
See an example of redim usage below:

dim Array1()
dim Array2()
dim Array3()
dim NewArray()

Counter = 0
setup a loop here that will iterate through all the instances of the arrays
redim NewArray(Counter,0)
For each X In Array1
redim NewArray(Counter,X)
   NewArray(Counter,X) = Array1(Counter,X)
next
For each Y In Array2
redim NewArray(Counter,Y+201)
   NewArray(Counter,Y+201) = Array2(Counter,Y+201)
next
For each Z In Array3
redim NewArray(Counter,Z+302)
   NewArray(Counter,Z+302) = Array3(Counter,Z+302)
next
counter=counter+1
loop		

Open in new window

0
 
StephenJRCommented:
billprew - that's neat, I didn't know you could do that.
0
 
DSmithVzCommented:
The Join/Split method shown above will fail if any item in your array contains a "|" symbol. You could make the split/join value something that's more unlikely to appear like ";dklsnfdioasd342". It depends on how you're using it. If if you never know what will be in the arrays, the below function works for any value:


Public Function MergeArrays(ByVal Array1, ByVal Array2, ByVal Array3)
	Dim AllArrays, intUBound, item, itemArray
	Dim aryReturn()
	
	AllArrays = Array(Array1, Array2, Array3)
	intUBound = 0
	For Each itemArray In AllArrays
		If IsArray(itemArray) Then
			For Each item In itemArray
				Redim Preserve aryReturn(intUBound)
				aryReturn(intUBound) = item
				intUBound = intUBound + 1
			Next
		End If
	Next

	MergeArrays = aryReturn
End Function

Open in new window

0
 
Bill PrewCommented:
==> The Join/Split method shown above will fail if any item in your array contains a "|" symbol.

Agreed, picking a delimiter that will not be in the data is important.  If there is no obvious normal character that will never show up, then you can pick a character like Chr(1) oe Chr(255) and have a pretty high confidence they won't be in the data.

~bp
0
 
DSmithVzCommented:
==> Agreed, picking a delimiter that will not be in the data is important.  If there is no obvious normal character that will never show up, then you can pick a character like Chr(1) oe Chr(255) and have a pretty high confidence they won't be in the data.

That is a good idea to use those fringe ASCII characters as a delimiter. Plus, I believe using Join/Split functions will have better performance than a loop. Not really noticeable for arrays of this size, but if they arrays have more than 10,000 items you'd probably notice the lag.
0
 
New_AlexAuthor Commented:
The Join/Split method shown above will fail, true,  but the other methods are not as efficient and simple. DSmith is right about Chr(1)  or Chr(255) or even Chr(13).

But I am going to split the points equally to all answers

NOTE: Any subsequent answers will not get any ponts
0
 
New_AlexAuthor Commented:
Thanks to all
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.