+ Reply to Thread
Results 1 to 6 of 6

Comparing two arrays what the best most efficient way

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Comparing two arrays what the best most efficient way

    Hi,

    I have two arrays. Both arrays contain file names. Both arrays are sorted in alphabetical order.

    WHat i want to do is the following:
    I want to compare one array to the other. If an entry in array1 is in array2 then i want to do "something". if an entry is in array1 but not in array2 then i dont care. If an entry is in array2 but not in array1 again i dont care.

    any ideas on an efficient way to do this?

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Comparing two arrays what the best most efficient way

    Hi welchs101, the following is an example of comparing two String Arrays.
    Sub CompareArrays()
        Dim strArray1() As String, strArray2() As String
        Dim aCnt As Long
        ReDim strArray1(5): ReDim strArray2(5)
        strArray1(1) = "Will"
        strArray1(2) = "this"
        strArray1(3) = "work"
        strArray1(4) = "?"
        strArray1(5) = "I don't know!"
        
        strArray2(1) = "Will"
        strArray2(2) = "this"
        strArray2(3) = "work"
        strArray2(4) = ""
        strArray2(5) = "I don't know!"
        
        For aCnt = 1 To 5
            If strArray1(aCnt) = strArray2(aCnt) Then
                Debug.Print "Do Something"
            Else
                Debug.Print "Do Nothing"
            End If
        Next aCnt
    End Sub
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Comparing two arrays what the best most efficient way

    thanks for responding.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Comparing two arrays what the best most efficient way

    No problem! And?

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Comparing two arrays what the best most efficient way

    Mordred, I think the implication is that the Arrays need not be of the same dimension and/or items in the same order ... ie "work" may be item 2 in strArray2.

    welchs101, you can either iterate the 2nd Array for each iteration of the 1st exiting upon match, e.g:

    Dim lngA1 As Long, lngA2 As Long
    Dim avArray1 As Variant, avArray2 As Variant
    '...arrays populated - assume sourced from Range and 2 dimensional
    For lngA1 = LBound(avArray1,1) To UBound(avArray1,1) Step 1
        For lngA2 = LBound(avArray2,1) To UBound(avArray2,1) Step 1
            If UCase(avArray1(lngA1,1)) = UCase(avArray2(lngA2,1)) Then
                'do something
                Exit For
            End If
        Next lngA2
    Next lngA1
    alternatively apply a Match function (binary given sort) to array2 (use Index if multi dimensional - assume given subject matter that case is irrelevant):

    Dim lngA1 As Long
    Dim avArray1 As Variant, avArray2 As Variant, vMatch As Variant
    '...arrays populated - assume sourced from Range and 2 dimensional
    For lngA1 = LBound(avArray1,1) To UBound(avArray1,1) Step 1
        vMatch = Application.Match(avArray1(lngA1,1),Application.Index(avArray2,0,1))
        If IsNumeric(vMatch) Then
            If UCase(avArray2(vMatch,1)) = UCase(avArray1(lngA1,1)) Then
                'do something
            End If
        End If
    Next lngA1
    Given the speed of Arrays I suspect iteration would be quicker than the Match approach.

    The fact that the arrays are sorted is helpful and with thought may permit some restrictions within the iteration, however, given the potential for missing items in either array it might not be as simple as appears at first glance.
    Last edited by DonkeyOte; 08-06-2011 at 03:35 AM. Reason: typos

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Comparing two arrays what the best most efficient way

    thanks guys...........very helpful indeed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

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 Friendly URLs by vBSEO 3.6.0 RC 1