Ed Matsuoka
asked on
Finding differences in two two-dimensional arrays
Hi Experts!
I did search here but didn't find this. I have two two-dimensional arrays (extracted from tables in Word docs), ARRAY1(0 to 100) and ARRAY2(0 to 300). Can I create an ARRAY3(0 to 300?) that only has those elements in ARRAY2 which are not in ARRAY1?
Thanks!
I did search here but didn't find this. I have two two-dimensional arrays (extracted from tables in Word docs), ARRAY1(0 to 100) and ARRAY2(0 to 300). Can I create an ARRAY3(0 to 300?) that only has those elements in ARRAY2 which are not in ARRAY1?
Thanks!
ASKER
Wow, thanks for replying so quickly! Just so you know, the arrays were pulled from two Word documents. Ideally, I'd like ARRAY3 to be in the same order as ARRAY2 if possible.
Okay, haven't tested this here, but the general idea would be:
»bp
Dim Array1(0 To 100)
Dim Array2(0 To 300)
Dim Array3(0 To 300)
' Populate Array1 and Arry2 here...
For i2 = LBound(Array2) To UBound(Array2)
Duplicate = False
For i1 = LBound(Array1) To UBound(Array1)
If Array2(i2) = Array1(i1) Then
Duplicate = True
End If
Next
If Duplicate = False Then
Redim Array3(UBound(Array3)+1)
Array3(UBound(Array3)) = Array2(i2)
End If
Next
»bp
You have two one-dimension arrays and want to create a third one-dimension array.
How much text is in each item of the array? If this is a lot of text, I would recommend putting the array1 items into a dictionary object and using the dictionary's .Exists() method. This way, you only iterate each array once and the order of the two arrays does not matter. You can populate the dictionary with a 1 (value/data) for each item.
How much text is in each item of the array? If this is a lot of text, I would recommend putting the array1 items into a dictionary object and using the dictionary's .Exists() method. This way, you only iterate each array once and the order of the two arrays does not matter. You can populate the dictionary with a 1 (value/data) for each item.
ASKER
When I run this code I only get the last element from ARRAY2 not in ARRAY1 into ARRAY3.
Seems to me I have run into that boundary condition, that UBound() is a little quirky when the array is empty, let me check...
»bp
»bp
ASKER
Here is a way of conceptualizing what I am doing:
I have ARRAY1 - ARRAY1(1)="X01", ARRAY1(2)="X06", ARRAY1(3)="X11").
I have ARRAY2 - ARRAY2(1)="X01", ARRAY2(2)="X02", ARRAY2(3)="X03", ARRAY2(4)="X06", ARRAY2(5)="X07", ARRAY2(6)="X11").
for ARRAY3 - ARRAY3(1)="X02", ARRAY3(2)="X03", ARRAY3(3)="X07")
I have ARRAY1 - ARRAY1(1)="X01", ARRAY1(2)="X06", ARRAY1(3)="X11").
I have ARRAY2 - ARRAY2(1)="X01", ARRAY2(2)="X02", ARRAY2(3)="X03", ARRAY2(4)="X06", ARRAY2(5)="X07", ARRAY2(6)="X11").
for ARRAY3 - ARRAY3(1)="X02", ARRAY3(2)="X03", ARRAY3(3)="X07")
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Bill!
That code is giving me variables in ARRAY3 that I also have in ARRAY1. Will this help?
array_variables.xlsx
That code is giving me variables in ARRAY3 that I also have in ARRAY1. Will this help?
array_variables.xlsx
I'm confused, are you sure you didn't start with something in Array3? I ran the code below, and got the values shown below, as you desired.
»bp
Sub Test3()
Dim Array1(0 To 100)
Dim Array2(0 To 300)
Dim Array3(0 To 300)
Dim i1, i2, i3, Duplicate
Array1(1) = "X01"
Array1(2) = "X06"
Array1(3) = "X11"
Array2(1) = "X01"
Array2(2) = "X02"
Array2(3) = "X03"
Array2(4) = "X06"
Array2(5) = "X07"
Array2(6) = "X11"
i3 = 0
For i2 = LBound(Array2) To UBound(Array2)
Duplicate = False
For i1 = LBound(Array1) To UBound(Array1)
If Array2(i2) = Array1(i1) Then
Duplicate = True
End If
Next
If Duplicate = False Then
i3 = i3 + 1
Array3(i3) = Array2(i2)
End If
Next
For i3 = 1 To UBound(Array3)
If Not IsEmpty(Array3(i3)) Then
Debug.Print Array3(i3)
End If
Next
End Sub
X02
X03
X07
»bp
ASKER
Thanks, Bill. I will check it out!
ASKER
Hi Bill!
It was my fault, the ARRAY1 was adding a leading space. I added a TRIM$ command and now it works perfectly. Thanks alot and have a great day!
It was my fault, the ARRAY1 was adding a leading space. I added a TRIM$ command and now it works perfectly. Thanks alot and have a great day!
»bp