Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Compare two multidimensional arrays

1. ## Compare two multidimensional arrays

I know how to:

1. Sort, get the count and compare two basic arrays.
2. Create and populate a multidimensional array.

What I need to know what is the difference between comparing 2 one dimensional arrays and comparing 2 two dimensional arrays?

Some how I think I need to declare the columns that I am comparing.

Here is the code that I am using to compare two basic arrays, after sorting them:

'Compare array1 to array 2

For Each strElementFirst In array1
blnExistsInSecond = False
For Each strElementSecond In array2
If strElementFirst = strElementSecond Then
blnExistsInSecond = True
Exit For
End If
Next
Next

'Compare array2 to array1

For Each strElementFirst1 In array2
blnExistsInSecond1 = False
For Each strElementSecond1 In array1
If strElementFirst1 = strElementSecond1 Then
blnExistsInSecond1 = True
Exit For
End If
Next
Next

Now comparing two multidimensional arrays do I need to include the max row count and column number after the array name?

Ex: array1(5,1) compare against array2(5,1)
and array1(5,2) compare against array2(5,2)

Any ideas on how to do this correctly?

Thanks!
SBsteve

2. ## Re: Compare two multidimensional arrays

I just did some more searching on Google. It looks like when I do a sort, it also has to be done per column, before I can do a compare.

Can anyone shed more light on this? In the mean time I'll keep searching for the answer.

SBsteve

3. ## Re: Compare two multidimensional arrays

If Join(arr1, "") = Join(arr2, "") Then
MsgBox "Match"
Else
MsgBox "Mismatch"
End If

As long as you dont need to know where they match...
Peace

4. ## Re: Compare two multidimensional arrays

Really appreciate this BeaconBlazer

5. ## Re: Compare two multidimensional arrays

No problem. Hope it helped you out.

Peace

6. ## Re: Compare two multidimensional arrays

I doubt this is what your trying to do Steve, but it's what it sounded like to me. Am I confused?

for iterate1 = 0 to ubound(arr1,1)
for iterate2 = 0 to ubound(arr1,2)
if arr1(iterate1,iterate2) = arr2(iterate1,iterate2) then msgbox "match"

next
next

Ken

7. ## Re: Compare two multidimensional arrays

Thanks much "BeaconBlazer" and Ken.

Ken I tried using yours, which I think will work in my situation, problem is it is failing because the multidimensional arrays haven't been properly sorted first.

I tried using a function that alphabetically sorts an array and I used that, but it didn't sort the arrays under test.

Any ideas on how to sort a multidimensional array?

Thanks!
SBsteve

8. ## Re: Compare two multidimensional arrays

I have read about two sort algorythms.. bubble and quicksort... one is easier and less robust and the other visa versa, but I don't recall which is which. If you Google those two terms with 'vbs' you should find some code you can work with.

Ken

9. ## Re: Compare two multidimensional arrays

OK will do, thanks again Ken!

SBsteve

10. ## Re: Compare two multidimensional arrays

I didn't know how long it would take me to find this, turned out to be fairly quick... this works for me
<font class="small">Code:</font><hr /><pre>
Sub Sorter( byRef arrArray )
Dim row, j,inc,test1,test2
Dim StartingKeyValue(6), NewKeyValue(6), swap_pos

For row = 0 To 4 'UBound( arrArray ) - 1
'Take a snapshot of the first element
'in the array because if there is a
'smaller value elsewhere in the array
'we'll need to do a swap.
For inc = 0 to 6
StartingKeyValue(inc) = arrArray ( row,inc)
next
For inc = 0 to 6
NewKeyValue(inc) = arrArray ( row,inc )
Next
swap_pos = row

For j = row + 1 to 5 'UBound( arrArray )
'Start inner loop.
test1 = arrArray ( j,0 )
test2 = NewKeyValue(0)
If arrArray ( j,0 ) &lt; NewKeyValue(0) Then
'This is now the lowest number -
'remember it's position.
swap_pos = j
For inc = 0 to 6
NewKeyValue(inc) = arrArray ( j,inc)
next
End If
Next

If swap_pos &lt;&gt; row Then
'If we get here then we are about to do a swap
'within the array.
test1 = StartingKeyValue(0)
test2 = NewKeyValue(0)
For inc = 0 to 6
arrArray ( swap_pos,inc ) = StartingKeyValue(inc)
Next
For inc = 0 to 6
arrArray ( row,inc ) = NewKeyValue(inc)
Next
End If
Next
End Sub
</pre><hr />

Ken

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.