Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join two arrays with different array numbers

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
Hi all,

I need to join two arrays with different array item numbers into one array. I tried to join them with the code listed below, but it will only join the array items of the two arrays up to the lower number of the array with the lower item number.

=======================================================

Dim arrItems, Array1, Array2

Array1 = Array("Text1","Text2","Text3","Text4","Text5")
Array2 = Array("Text6","Text7","Text8","Text9")

joinData Array1, Array2

For Each objItem In arrItems
Wscript.Echo objItem
Next

Sub joinData(arrData1, arrData2)
Dim k, i, j
arrItems = Array("") : k = 0
For i = LBound(arrData1) To UBound(arrData1)
For j = LBound(arrData2) To UBound(arrData2)
If i = j Then
arrItems(k) = arrData1(i) & ";" & arrData2(j)
k = k + 1
ReDim PreServe arrItems(k)
Exit For
End If
Next
Next
End Sub

======================================================

When I ran the above code, I got the following result:

Text1;Text6
Text2;Text7
Text3;Text8
Text4;Text9

What I want is the following:

Text1;Text6
Text2;Text7
Text3;Text8
Text4;Text9
Text5;

Even if the second array has only 4 items, I still want the final array to contain the items from the first array with blank data.

If there any way to join the two arrays to contain all items from the array with higher item number even when they have different array item numbers?

Thanks!

CluM09
 
How about:
Code:
Function joinData(arrData1, arrData2)

Dim newArray(), counter

counter = 0
Do While counter <= UBound(arrData1) And counter <= UBound(arrData2)
  Redim Preserve newArray(counter)
  newArray(counter) = ""
  If counter <= UBound(arrData1) Then
    newArray(counter) = arrData1(counter)
  End If
  If counter <= UBound(arrData2) Then
    newArray(counter) = newArray(counter) & ";" & arrData2(counter)
  End If
  counter = counter + 1
Loop

joinData = newArray

End Function

or

Code:
Function joinData(arrData1, arrData2)

Dim newArray(), counter
ReDim newArray(UBound(arrData1))

For counter = LBound(arrData1) to UBound(arrData1)
  newArray(counter) = arrData1(counter)
Next

For counter = LBound(arrData2) to UBound(arrData2)
  newArray(counter) = newArray(counter) & ";" & arrData2(counter)
Next

joinData = newArray

End Function

As you can see, there's more than one way to accomplish this and more than one way to approach a solution.

Lee
 
trollacious,

Thank you for the help!

The solution I am looking for is the second option. The first option develops the same result as my original code.

I am glad that I can get help for the array problem I have.

Thanks again!

CluM09
 
The first example I provided that you said generates the same as your original code has an error in it. The loop should begin
Code:
Do While counter <= UBound(arrData1) Or counter <= UBound(arrData2)

Sorry about that. This should provide the same functionality as the second loop that you indicated works fine. With small arrays, it won't matter which version you use, but for larger ones you'll find the single loop will be a lot faster than 2 loops.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top