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!

How to add 2 array together

Status
Not open for further replies.

denis60

Technical User
Apr 19, 2001
89
CA
Hi!
Is it possible to add 2 array of the same width like:
arr3 = arr1 + arr2
 
if u want the L value aray to have the size of sum of the two args
ex

ar1=(2,2,1)
ar2=(1,4)
'expected otput ar3(2,2,1,1,4) if so do this
dim ar3() as integer ' dynamic array

dim lar1
dim lar2
....
lar1=ubound(ar1)
lar2=ubound(ar2)
redim ra3(lar1+lar2) as integer

'use a loop to copy the value




 
Ok, i don't have choice, i have to use a "for loop" statement to do that. Nothing faster like a strait liner command

 
use this to do a fast copy

Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)


Public Sub MemAppend(mDest() As Byte, Source1() As Byte, source2() As Byte)
Dim i As Integer

CopyMemory mDest(0), Source1(0), 2
CopyMemory mDest(2), source2(0), 2

For i = 0 To UBound(mDest)
Debug.Print mDest(i)
Next

End Sub


Public Sub test()
Dim a(1) As Byte
Dim b(1) As Byte
Dim c(3) As Byte

a(0) = 1
a(1) = 2
b(0) = 3
b(1) = 4

MemAppend c, a, b
End Sub
 
What sort of add are you looking to do?

Are you trying to make a new array, only longer (append array B to array A)?

Or are you trying to add each element in the first array to the corresponding element in the second array?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top