Click to See Complete Forum and Search --> : Comparing two Arrays


srinika
September 28th, 2001, 02:23 AM
There are 2 arrays (Say A & B). Both of them having some similar elements. I need to put similar elements of A & B in to another array(say C) and the Elements which are in A but not not in B in to another array (say D).
eg.
Elements in A: a1,a2,p,q,
Elements in B: b1,b2,p,q
After process
Elements in C: p,q
Elements in D: a1,a2
What is the most efficient way to handle this?
Srinika

Cakkie
September 28th, 2001, 02:42 AM
Dim T as integer, intC as integer, intD as integer

for t=0 to ubound(arrayA)

if arrayA(t) = arrayB(t) then
intC = intC + 1
Redim Preserve arrayC(intC)
arrayC(intC-1) = arrayA(t)
else
intD = intD + 1
Redim Preserve arrayD(intD)
arrayC(intD-1) = arrayA(t)
end if

next t




Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

Cimperiali
September 28th, 2001, 03:04 AM
You're looking only in same position of both arrays...

Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater

Cakkie
September 28th, 2001, 03:24 AM
Ok wiseass (kidding)

private Sub Command1_Click()

Dim t as Variant, r as Variant
Dim colC as new Collection, colD as new Collection

Dim ArrayA, ArrayB
Dim ArrayC(), ArrayD()

ArrayA = Array("1", "2", "6", "7")
ArrayB = Array("3", "4", "6", "7")

on error resume next
' check to see which are in A and B
for Each t In ArrayA
for Each r In ArrayB
If t = r then
colC.Add t, CStr(t)
End If
next r
next t

' check to see which aren't in A and B
for Each t In ArrayA
r = null
r = colC(CStr(t))
If IsNull(r) then
colD.Add t
End If
next t

' put them in the arrays
ReDim ArrayC(colC.Count)
ReDim ArrayD(colD.Count)

r = 0
for Each t In colC
ArrayC(r) = t
r = r + 1
next t

r = 0
for Each t In colD
ArrayD(r) = t
r = r + 1
next t

End Sub



Happy now? It even prevents doubles in the arrays

Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

Cimperiali
September 28th, 2001, 03:36 AM
Trying to give you an excelent, codeguru replied to me:

"We encountered a problem!

The server encountered an error and cannot complete your request.
The error reported was:

You have already reached the limit of 5 today.
This restriction has been imposed to prevent misuse of the system.
Note: to the program, the time frame for a day is based on California time."
...

Hope someone here will rate your code!
ps: Chris rated me today!





Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.

The Rater