Solved

# vbScript glue 3 arrays into one

Posted on 2011-10-20
Medium Priority
426 Views
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?

0
Question by:New_Alex
• 2
• 2
• 2
• +3

LVL 24

Assisted Solution

StephenJR earned 664 total points
ID: 36999729
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

LVL 10

Expert Comment

ID: 36999730
You can redim an array to the size of each arrays bounds and then populate it.
0

LVL 11

Assisted Solution

ID: 37002480
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
``````
0

LVL 59

Accepted Solution

Bill Prew earned 668 total points
ID: 37003981
Try this:

``````Dim AllArray()
AllArray = Split(Join(MyArray1,"|") & "|" & Join(MyArray2,"|") & "|" & Join(MyArray3,"|") ,"|")
``````
~bp
0

LVL 24

Expert Comment

ID: 37005358
billprew - that's neat, I didn't know you could do that.
0

LVL 2

Expert Comment

ID: 37006565
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
``````
0

LVL 59

Expert Comment

ID: 37007133
==> 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

LVL 2

Expert Comment

ID: 37009926
==> 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

LVL 1

Author Comment

ID: 37014278
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

LVL 1

Author Closing Comment

ID: 37087560
Thanks to all
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
###### Suggested Courses
Course of the Month16 days, 18 hours left to enroll