Results 1 to 9 of 9

Thread: Compare two arrays in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    3

    Compare two arrays in VB6

    I have two arrays, each have about 10000 text values. I want to compare this two arrays and get list of diference betwen them. Now i have loop and i take first field in array1 then compare with every field in array2, then i take second field in array1 and compare... and so on. This take to much time. Is any faster way? Thanks!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Compare two arrays in VB6

    Do you just need to know if they are not identical? Or do you need to know which specific items are different?
    Also, are the arrays sorted?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Compare two arrays in VB6

    there is a function that I ahve just used on the forum

    that may be the root of such a solution

    you would only have to loop through the arrays once each

    the function would return the differences

    the problem is that each array may have different things in

    not just that one array has different things in

    you original searches would take 10000*10000*2 + matched*10000*2 for the same reasons

    mine would take 10000*2 + matched*10000*2 so mines a bit faster

    both searches would have to store the matching values in order to remove them from a final amalgamated array

    I have a thought you could simply put the arrays together and remove the matching entries

    that would require the arrays to be sorted an over head I know

    a simple insertion sort (the worst kind) would take 200010000
    and the removal of the matched would take an additional 20000

    this merge version wins hands down when it comes to arrat transactions

    the new function verion comes in a near second compared to you original

    so

    merge the arrays into one huge array ordering as you go
    then set up a number of pointers p1 and p2 when the contents at p1 and p2 are the same
    think about removing them but only from p2 as there may be more than one match
    flag tat you have need ed to delete and move p2 delete as appropriate keeping the flag status
    when the p1 and p2 are different and the deletion flag is set delete at p1
    clear the flag and point p1 at p2 move p2 to the next position
    start testing again
    when you have arrived at the endof the array the matches have all been deleted!


    the new function model
    loop through array1 producing sub srrays from the function arr=filter (array2,array1(i))
    put all of the arr values into a to_delete_control array
    then loop throu array2 with arr=filter(array1,array2(i))
    collecting all of the arr elements to the to_delete_control array
    now add array and array2 into an unsorted colleced_array
    walk through the to_delete array remove the matching elements
    you could use lists or arrays
    the advantage of lista is that you could remove the matches from the lists with ease
    everything just shuffles back into place
    but do bot remove the matches too soon, but remember you must remove from both arrays or lists eventually

    your version - well you know it works

    its late here and i am getting into trouble typing away in the bed room so its goodnight from me

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Compare two arrays in VB6

    This is one of those "If I was going there, I wouldn't start from here" cases. It sounds like a 'traditional Batch Update' type of problem. With the number of data items you have, it may be better to use a Database; create two Tables, one for each array and then use SQL to Order them and then compare the two.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    3

    Re: Compare two arrays in VB6

    Actually I have the main base of 10000 songs and destination base of songs with xxx files. Now i want to figure out what i must delete in destination base (becouse not exist in main base) and what is missing in destination base.

    Now I create two arrays with names of the songs, and compare one array with another and crete list of files that i need to delete them and list of files that i need to copy them. I do this quite often and compareing this two arrays take about 10 min., ... which is too long.

    Is there any way to just sync two folders?
    Or exists any command which give me diference betwen two arrays?

  6. #6
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Compare two arrays in VB6

    the idea of usinga database (acess) is a fine one, but i thought you wanted to do the programming.

    there exists in excell 2 functions that can return exactly what you want

    they are in the wizard section of excell

    here to help (with either option although one of them belongs ina different section of the forum)

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Compare two arrays in VB6

    A folder synchronization might look like the following. The following purposely excludes recursive comparisons into any subfolders of the target folders. Supply appropriate values for MainBase & DestinationBase
    Code:
    Option Explicit
    
    
    Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    Private Const INVALID_HANDLE_VALUE As Long = -1&
    
    Private Sub SyncrhonizeFolders(SourceFolder As String, DestFolder As String, srcFilesRtn As Collection, dstFilesRtn As Collection, dualDirection As Boolean)
    
        Dim sFile As String
    
        If Right$(SourceFolder, 1) <> "\" Then SourceFolder = SourceFolder & "\"
        If Right$(DestFolder, 1) <> "\" Then DestFolder = DestFolder & "\"
    
        Set srcFilesRtn = New Collection
        Set dstFilesRtn = New Collection
        
        sFile = Dir$(SourceFolder & "*.*", vbArchive Or vbHidden Or vbReadOnly Or vbSystem)
        Do Until sFile = vbNullString
            If (GetAttr(SourceFolder & sFile) And vbDirectory) = 0 Then ' exclude subfolders
                If GetFileAttributes(DestFolder & sFile) = INVALID_HANDLE_VALUE Then srcFilesRtn.Add sFile
            End If
            sFile = Dir$()
        Loop
        Dir$ vbNullString
        
        If dualDirection Then
            sFile = Dir$(DestFolder & "*.*", vbArchive Or vbHidden Or vbReadOnly Or vbSystem)
            Do Until sFile = vbNullString
                If (GetAttr(DestFolder & sFile) And vbDirectory) = 0 Then ' exclude subfolders
                    If GetFileAttributes(SourceFolder & sFile) = INVALID_HANDLE_VALUE Then dstFilesRtn.Add sFile
                End If
                sFile = Dir$()
            Loop
            Dir$ vbNullString
        End If
    
    End Sub
    
    Private Sub Command1_Click()
    
        ' sample call
    
        Dim missingSrcFiles As Collection
        Dim missingDstFiles As Collection
        Dim X As Long
        
        Call SyncrhonizeFolders(MainBase, DestinationBase, missingSrcFiles, missingDstFiles, True)
        
        ' press Ctrl+G to see the print out
    
        Debug.Print "files in source & not in destination"
        If missingSrcFiles.Count = 0 Then
            Debug.Print vbTab; "none"
        Else
            For X = 1 To missingSrcFiles.Count
                Debug.Print vbTab; missingSrcFiles.Item(X)
            Next
        End If
        Debug.Print "files in destination & not in source"
        If missingDstFiles.Count = 0 Then
            Debug.Print vbTab; "none"
        Else
            For X = 1 To missingDstFiles.Count
                Debug.Print vbTab; missingDstFiles.Item(X)
            Next
        End If
        
    End Sub
    When the routine returns you can decide what you want to do with the files in one or both of the collections

    Edited: Here is a designed class with example project in vb6 from a site I consider respectable
    Last edited by LaVolpe; Dec 31st, 2011 at 04:01 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    3

    Re: Compare two arrays in VB6

    THANKS GUYS!!! This is what I need. I must do some tests, but it works very fast now, like I want

    Just one more questions. I have always one destination folder, but in source folder i have 3 subfolders. Sometimes I must include files of this subfolders in synchronizations. So i try this

    sFile = Dir$(SourceFolder & "*.*", vbArchive Or vbHidden Or vbReadOnly Or vbSystem) & Dir$(SourceFolder & "test\*.*" , vbArchive Or vbHidden Or vbReadOnly Or vbSystem)

    I want combine files in folder and files in subfolder into sFile! But it doesn't go that way...

    Any sugestions?
    Thanks!

  9. #9
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Compare two arrays in VB6

    Nope. You'd have to modify whatever code you may now be using to process multiple folders. Personally, I think it may be worthwhile, and far less confusing, to simply call your routine once for each destination you will process, do what you need with the files before moving on to the next destination
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width