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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenate Arrays 1

Status
Not open for further replies.

psandekian

Programmer
Oct 12, 2000
48
US
Does anyone know if VB 6 has a function to concatenate two arrays into one?

Thanks.
Patty [sig][/sig]
 
Try the Join function:

Join(SourceArray[, Delimiter])

SourceArray is a one-dimensional array containing the strings to be concatenated.

Delimiter is an optional parameter that lists the delimiter to be used to separate the substrings in the array when concatenating them into the single string.
By default (if no delimiter is specified), a space is used.

This description is also available in MSDN.

Hope this helps.

Steve

[sig][/sig]
 
Steve,

Thanks but I was hoping to take two arrays of numbers and put them end to end in another array. Will this do the same thing? I saw this in MSDN but I didn't think this is what I wanted.

Thanks.
Patty [sig][/sig]
 
As far as i know there is no function to do this, but its not a difficult task.

Private Array1() As Long
Private Array2() As Long
Private Counter As Long

Private Sub Command1_Click()
For Counter = 0 To UBound(Array1) - 1
Array1(Counter) = Counter
Next Counter
For Counter = 0 To UBound(Array1) - 1
List1.AddItem Array1(Counter)
Next Counter
End Sub

Private Sub Command2_Click()
For Counter = 0 To UBound(Array2) - 1
Array2(Counter) = Counter
Next Counter
For Counter = 0 To UBound(Array2) - 1
List2.AddItem Array2(Counter)
Next Counter

End Sub

Private Sub Command3_Click()
Dim UB1 As Long
Dim UB2 As Long

UB1 = UBound(Array1)
UB2 = UBound(Array2)
ReDim Preserve Array1(UB1 + UB2)
For Counter = UB1 To UB1 + UB2
Array1(Counter) = Array2(Counter - UB1)
Next Counter

For Counter = 0 To UBound(Array1) - 1
List3.AddItem Array1(Counter)
Next Counter

End Sub

Private Sub Form_Load()
ReDim Array1(25)
ReDim Array2(35)
End Sub
[sig]<p>Ruairi<br><a href=mailto:ruairi@logsoftware.com>ruairi@logsoftware.com</a><br>Experienced with: <br>
VB6, SQL Server, QBASIC, C(unix), MS Office VBA solutions<br>
ALSO: Machine Control/Automation using GE and Omron PLC's and HMI(human machine interface) for industrial applications[/sig]
 
Sorry, i accidentaly posted that before i was done describing it. I'm not sure how you want to use this so i just made a simple example project that should answer any questions you have, just start a new project and draw 3 command buttons and 3 list boxes on the form, then paste this code in. It shows you how to use the UBound() function for both arrays to get the new array size how to copy from one to the end of the other. The Preserve keyword in the redim statement keeps the original array values intact when it is resized. [sig]<p>Ruairi<br><a href=mailto:ruairi@logsoftware.com>ruairi@logsoftware.com</a><br>Experienced with: <br>
VB6, SQL Server, QBASIC, C(unix), MS Office VBA solutions<br>
ALSO: Machine Control/Automation using GE and Omron PLC's and HMI(human machine interface) for industrial applications[/sig]
 
OK, ready for a hack?

In a VB .bas module, put the following API declaration:

Public Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; (Destination As Any, Source As Any, ByVal Length As Long)

This API call can be used like:

Private Sub Form_Load()

Dim a(10) As Long
Dim b(10) As Long
Dim c() As Long

For I = 0 To 10
'a will have values 0 to 10
a(I) = I
'b will have value 11 to 21
b(I) = I + 11
Next I

c() = CopyArray(a, b)

End Sub

Public Function CopyArray(Source1() As Long, Source2() As Long) As Long()

Dim Size1 As Long
Dim Size2 As Long
Dim Result() As Long

'number of bytes in each array
'note that a Long uses 4 bytes
Size1 = (UBound(Source1) - LBound(Source1) + 1) * 4
Size2 = (UBound(Source2) - LBound(Source2) + 1) * 4

'set the size of the resulting array
ReDim Result(UBound(Source1) + UBound(Source2))

CopyMemory Result(LBound(Result)), Source1(LBound(Source1)), Size1
CopyMemory Result(UBound(Source1) + 1), Source2(LBound(Source2)), Size2

CopyArray = Result

End Function

Now for an explanation of the CopyMemory calls.
On a low level, copy memory takes the physical address of the destination variable and the source variable, along with the number of bytes to copy.
So, first you copy Size1 bytes of memory from the beginning address of the first array -- located at Source1(LBound(Source1)) -- to the beginning address of the Result array -- located at Result(LBound(Result)).
Next, you copy Size2 bytes of memory from the beginning address of the second array -- located at Source2(LBound(Source2)) -- to the next address of the Result array -- located at the address of the array element in Result just after the end of Source1, thus the memory address would be Result(UBound(Source1) + 1).

Be careful of how you implement this API call -- if you use it wrong you will Dr. Watson of GPF your machine!

There must be an easier/higher-level way to do this, but the couple of other ways I tried didn't work, and this way is more fun anyhow ;-)

Good luck!

Steve [sig][/sig]
 
Patty,

The first of these functions is just a way to generate some random values in two random sized arrays so we can actually run the second function.

The second function &quot;concatenates&quot; the two arrays into the third. Each of the first two arrays is sized randomly, so the second function deals with the size in an orderly manner (e.g. dynamically resizes the resultant asd necessary) Note that this is not the most efficient code, as it is only intended to 'illustrate' the process. Alos, note that the arrays here are all decalred as variant. This also decreases the 'efficiency', however it does allow a great deal of flexability in the process, since we don't need to know what we are concatenating.

Hopefully, this will be of use to you.




(Public Function basConCatArray(MyArray1 As Variant, MyArray2 As Variant, Myarray3 As Variant)

Dim Idx As Integer
Dim Jdx As Integer

'This concatenates the two previouslky created arrays
ReDim Myarray3(0)
For Idx = 0 To UBound(MyArray1)
Myarray3(Idx) = MyArray1(Idx)
ReDim Preserve Myarray3(Idx + 1)
Next Idx

For Jdx = 0 To UBound(MyArray2)
Myarray3(UBound(Myarray3)) = MyArray2(Jdx)
ReDim Preserve Myarray3(UBound(Myarray3) + 1)
Next Jdx
ReDim Preserve Myarray3(UBound(Myarray3) - 1)

Debug.Print UBound(MyArray1), UBound(MyArray2), UBound(Myarray3)

End Function



Public Function basMakeArrays()

'This is just a Wrapper so we can test the
'concatenation of the arrays.

'WE need to Define the arrays somewhere
'This is just a convenient place
Dim MyArray1() As Variant
Dim MyArray2() As Variant
Dim Myarray3() As Variant

Dim Idx As Integer
Dim Jdx As Integer

'This just populates the arrays so we can see what works
ReDim MyArray1(0)
For Idx = 0 To RND() * 12
MyArray1(Idx) = RND()
ReDim Preserve MyArray1(Idx + 1)
Next Idx
ReDim Preserve MyArray1(Idx - 1)

ReDim MyArray2(0)
For Idx = 0 To RND() * 7
MyArray2(Idx) = RND()
ReDim Preserve MyArray2(Idx + 1)
Next Idx
ReDim Preserve MyArray2(Idx - 1)

Call basConCatArray(MyArray1, MyArray2, Myarray3)

End Function



Public Function basConCatArray(MyArray1 As Variant, MyArray2 As Variant, Myarray3 As Variant)

Dim Idx As Integer
Dim Jdx As Integer

'This concatenates the two previouslky created arrays
ReDim Myarray3(0)
For Idx = 0 To UBound(MyArray1)
Myarray3(Idx) = MyArray1(Idx)
ReDim Preserve Myarray3(Idx + 1)
Next Idx

For Jdx = 0 To UBound(MyArray2)
Myarray3(UBound(Myarray3)) = MyArray2(Jdx)
ReDim Preserve Myarray3(UBound(Myarray3) + 1)
Next Jdx
ReDim Preserve Myarray3(UBound(Myarray3) - 1)

Debug.Print UBound(MyArray1), UBound(MyArray2), UBound(Myarray3)

End Function
[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Thanks Guys! That's a lot to read over and absorb! I was hoping VB would have something like JScript did (found in MSDN)
Code:
function ConcatArrayDemo(){
   var a, b, c, d;
   a = new Array(1,2,3);
   b = &quot;JScript&quot;;
   c = new Array(42, &quot;VBScript);
   d = a.concat(b, c);
   //Returns the array [1, 2, 3, &quot;JScript&quot;, 42, &quot;VBScript&quot;]
   return(d);
}

Thanks!
Patty
[sig][/sig]
 

To combine array a and b into c:

For m = 0 To UBound(a) - 1
c(m) = a(m)
Next

n = m

For m = 0 To UBound(b) - 1
c(n) = b(m)
n = n + 1
Next

If you use Option Base 1 then change the zeros to ones and drop the minus one in the For statement.
[sig]<p> Tarek<br><a href= > </a><br>The more I learn, the more I need to learn![/sig]
 
Tarek,

I used this format also. It seemed to be the most efficent for my use.

Thanks everyone for your input!
Patty [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top