Results 1 to 7 of 7

Thread: One of a kind....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    Hiyas,

    I am loading a text file into a listbox, and the text file may have duplicates of a word, which I don't want I only want one of this word added to the list and the rest ignored sort of thing.

    Now I am currently doing this by setting the sorted property to true on the listbox and then looping through the entire list checking one item with the item below it to make sure the text is not the same and if it is, remove that item.

    This works fine, but it is toooooo slow a method when dealing with a medium or so size text file, is there any quicker way to do this?.

    thanx for any help.

  2. #2
    Guest
    Try this:

    Code:
    For i = 0 To List1.ListCount - 1
        If i = List1.ListCount Then Exit Sub
        For j = 0 To List1.ListCount - 1
            If j <> List1.ListCount Then
                If LCase$(List1.List(i)) = LCase$ _
                (List1.List(j)) And i <> j Then
                    List1.RemoveItem (j)
                    j = 0
                End If
            End If
        Next j
    Next i

  3. #3
    New Member
    Join Date
    Aug 2000
    Posts
    11
    Would it be quicker to load all the information in the text file into an array first, scan through that removing duplicates and then add them all to the list box?

    It might be slightly more code, but the operation should be faster...

    Give me five lines written by the most honourable of men, and I shall find in them an excuse to hang him. - Cardinal Richelieu

    Ben Stappleton
    VB6E SP4

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    Hiyas

    thanx for helping,

    mathew thats basically the type of code I am already using, scanning through the list for a duplicate, and currently the method is too slow when you are dealing with alot of items.

    this is the code I am using:

    Code:
        Dim I As Long
        Dim P As Long
    
        On Error GoTo Done
    
        For I = 0 To List1.ListCount - 1
            P = I + 1
    
    Stuff:
            If LCase(List1.List(I)) = LCase(List1.List(P)) Then
                List1.RemoveItem (P)
                GoTo Stuff
            End If
        Next I
    
    Done:
    I don't know which one would be quicker though.
    as for arrays wouldn't that be longer? seeing as the listbox's sorted property makes it so the list is in alphobetical order, so that if an item was a duplicate it would be just below the orignal of the word, where as I would have to loop through every single array for each item to find the duplicate where as with the listbox when you have the sorted property to true all you have to do is compare it to the list item below it to find out if it is a duplicate, but still this method is too slow for my liking.

    [Edited by Crypt on 08-25-2000 at 03:43 AM]

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Try this. This search is case independent so you don't have to convert the string to LCase or UCase.
    Code:
    Private Declare Function SendMessageString _
     Lib "user32" Alias "SendMessageA" ( _
     ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     ByVal lParam As String) As Long
    
    Private Const LB_FINDSTRING = &H18F
    
    Private Sub Command1_Click()
        Dim nRetVal As Long
        Do
            nRetVal = SendMessageString(List1.hwnd, LB_FINDSTRING, -1&, "StringToMatch")
            If nRetVal > -1& Then
                List1.RemoveItem nRetVal
            End If
        Loop While nRetVal > -1&
    End Sub
    Good luck!

  6. #6
    New Member
    Join Date
    Aug 2000
    Posts
    11
    There is information on sorting techniques elsewhere on this site, and when I had to do something like this before, it was much quicker to load the text file into an array, sort it and then you can go through the array setting duplicate values to null (as you said, all duplicates will be next to each other).

    You then add all the values in the array to the listbox if they're not null. An advantage of this is that the listbox does not have to have .sorted set to true, which speeds up the adding proccess (unless you're going to be adding to it dynamically later and want these entries sorted as well).

    I was doing it for a list of system users (about 1400 people), and this method worked out faster. It could just be that all the rest of the methods I used were so crap that this simply looked good by comparison.

    It takes more code than working solely with the list box, but array operations are faster.
    Give me five lines written by the most honourable of men, and I shall find in them an excuse to hang him. - Cardinal Richelieu

    Ben Stappleton
    VB6E SP4

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    thanx for all the help on this topic

    I think I will use Joacim's method, but rather than searching for duplicates after I have loaded the text file into the listbox, actually search for that item before I add it to the list.
    so I'll only add the item if it doesn't exist, that will probably be the quickest way I think,

    thanx again for all the help.

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