?
Solved

vbScript glue 3 arrays into one

Posted on 2011-10-20
10
Medium Priority
?
426 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:New_Alex
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 24

Assisted Solution

by:StephenJR
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

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

Assisted Solution

by:ScriptAddict
ScriptAddict earned 668 total points
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		

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 59

Accepted Solution

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

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

Open in new window

~bp
0
 
LVL 24

Expert Comment

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

Expert Comment

by:DSmithVz
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

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
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

by:DSmithVz
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

by:New_Alex
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

by:New_Alex
ID: 37087560
Thanks to all
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question