CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2005
    Posts
    59

    Unhappy [2005] Array Duplicates

    Hello again.

    Let me get straight to the point.

    I have a combobox, which I use to store the ComboText as well as the number of times that particular word(s) was clicked on inthat session. The code I use for the Combobox looks like:
    Code:
        Private Sub cboSSCourse_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSSCourse.SelectedIndexChanged
    
      If cboSSCourse.SelectedIndex > -1 Then 'if something selected
    
                iSS = cboSSCourse.SelectedIndex 'set = to sel index
                SSSubCount(iSS) += 1
                SSSubs(iSS) = cboSSCourse.Text & " " & CStr(SSSubCount(iSS)) & ";"
            End If
    Then, I save this info into a text file with the following sub, when I exit the app:
    Code:
        Private Sub SSWriteToFile(ByVal Path As String)
            Dim stream_writer As IO.StreamWriter
            Dim i As Integer
    
            ' Save the file.
            stream_writer = New IO.StreamWriter(Path, True)
            For i = 0 To SSSubs.Length - 1
                stream_writer.Write(SSSubs(i))
            Next
            stream_writer.Close()
        End Sub

    This produces a Text file that looks like this:
    Code:
    ASP 2 Advanced 2;Corel Draw 10 Foundation 1;Dreamweaver Advanced 1;Business Finance 1;Director Foundation 1;Excel 2000 Advanced 1;ASP 2 Advanced 5;Dreamweaver Advanced 1
    This works perfectly.

    Now, what I need to do is to obtain a list of all the duplicates, alongwith their associated "click times" (which is the number next to the text, before the semi colon)

    So, I did this:

    Code:
        Private Sub SSGetSubFileInfo(ByVal Path As String)
            Dim stream_reader As IO.StreamReader
            Dim i As Integer
            'Dim j As Integer
    
            stream_reader = New StreamReader(Path, True)
            SSubText = stream_reader.ReadToEnd().Split(";")
    
            stream_reader.Close()
            ReDim SSFileSub(SSubText.Length - 1)
            ReDim SSFileCount(SSubText.Length - 1)
    
            For i = 0 To SSubText.Length - 1
                SSFileSub(i) = SSubText(i).Substring(0, SSubText(i).LastIndexOf(" "))
                SSFileCount(i) = SSubText(i).Substring(SSubText(i).LastIndexOf(" "), 2)
    
                If SSOrganiseList(SSFileSub) Then
                    Console.WriteLine(SSFileSub(i))
                    Console.WriteLine(SSFileCount(SSDup(i)))
                End If
            Next
    
        End Sub
    This sub gets all the items within the textfile, and calls this function (to check for duplicates):
    Code:
        Private Function SSOrganiseList(ByVal arr As Array) As Boolean
    
            ReDim SSDup(arr.Length - 1)
            For i As Integer = 0 To arr.Length - 1
                If Not arr(i) Is Nothing Then
                    Dim l As Integer = Array.LastIndexOf(arr, arr(i))
    
                    If l <> i Then
                        '           SSDupsFound += 1
                        SSDup(i) = i
                        '   SSDup2 = l
                        Return True
                        ' Return SSDupsFound
                    End If
                End If
            Next
            Return False
    
        End Function
    The problem is, once it has found a duplicate item, it stops counting.
    What I'm trying to say is, it finds ASP 2 Advanced as a duplicate, with its "click time", it finds Dreamweaver Advanced as a duplicate, but doesn't find its "click time"

    I need to find the items, then add together all their respected click times, for example:
    ASP 2 Advanced has 5, then 2
    So the total must be 7
    Dreamweaver Advanced has 1 and 1 so the total must be 2

    Any ideas on how I can achieve this?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2005] Array Duplicates

    Loop thru the first one like you are. That is fine.

    Put another loop inside of that loop that checks each in the list, adding +1 when it finds a match.

    When it finishes, you will have a count of each word on the list
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2005
    Posts
    59

    Re: [2005] Array Duplicates

    Thanx for your advice. The problem is, I tried that - if you look closely I had a variable named SSDupsFound. The problem seems to be that it only checks once, but it does give me all the duplicate names, just not their numbers.

    Any other ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2005] Array Duplicates

    You have only one loop
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2005] Array Duplicates

    Looks like 2 loops to me, one in the calling routine and another inside the function which has been called from inside the first loop.

    I did not look real closely but at a glance it appears that the return true is the problem. When it finds a dupe it exists the function rather than going through the rest of the items to see if there are any more instances of that item.

  6. #6
    Join Date
    Sep 2005
    Posts
    59

    Re: [2005] Array Duplicates

    OK, thanx DataMiser.
    I have now replaced the Function with Sub.

    I did this in there (it's basicall the same still):
    Code:
     Private Sub SSOrganiseList(ByVal arr As Array)
    
            ReDim SSDup(arr.Length - 1)
            For i As Integer = 0 To arr.Length - 1
                If Not arr(i) Is Nothing Then
                    Dim l As Integer = Array.LastIndexOf(arr, arr(i))
    
                    If l <> i Then
                           SSDup(i) = l
                                         
                    End If
                End If
            Next
    End Sub
    
       Private Sub SSGetSubFileInfo(ByVal Path As String)
            Dim stream_reader As IO.StreamReader
            Dim i As Integer
            'Dim j As Integer
    
            stream_reader = New StreamReader(Path, True)
            SSubText = stream_reader.ReadToEnd().Split(";")
    
            stream_reader.Close()
            ReDim SSFileSub(SSubText.Length - 1)
            ReDim SSFileCount(SSubText.Length - 1)
    
            For i = 0 To SSubText.Length - 1
                SSFileSub(i) = SSubText(i).Substring(0, SSubText(i).LastIndexOf(" "))
                SSFileCount(i) = SSubText(i).Substring(SSubText(i).LastIndexOf(" "), 2)
    
                SSOrganiseList(SSFileSub)
                Console.WriteLine(SSFileSub(SSDup(i)))
                Console.WriteLine(SSFileCount(SSDup(SSDup(i))))
    
            Next
    
        End Sub
    Now, it returns:
    Code:
    ASP 2 Advanced
    2
    ASP 2 Advanced
    2
    ASP 2 Advanced
    2
    ASP 2 Advanced
    2
    ASP 2 Advanced
    2
    ASP 2 Advanced
    2
    ASP 2 Advanced
    5
    ASP 2 Advanced
    5
    I can see it does get the associated numbers, but only for the one duplicated entry, not the secondone which is Dreamweaver.

    I don't understand why I need to use more loops. With that logic, if I have 2 duplicated entrries, I ned 2 loops - in this case I have a possible 92 duplicated entries, so that means 92 loops?

  7. #7
    Join Date
    Apr 2008
    Posts
    82

    Re: [2005] Array Duplicates

    i was going to say this on vbforums, where the same question was asked http://www.vbforums.com/showthread.php?t=562886 . why allow duplicates at all? when someone clicks on one of the items just update the count when the click happens.

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2005] Array Duplicates

    Basically you need one loop that looks at every item in the array. Then you need the second loop inside the first. the second loop compares each item in the array to the item currently active in the outter loop. This will work for any number of dupes if done correctly but it does seem like an odd way to go about it.

    As Oblio mentioned
    I would strongly suggest updating the values in the array rather than writting dupes and trying to sort them out later.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [2005] Array Duplicates

    I just looked at the posts here in more detail. It appears that there are or could be many sessions written to that text file. If the intention is to keep each session recorded seperately but then be able to grab the totals for each item then what you may want to consider is a second array.

    read the file into the array as you are doing now. Create a second array with the same structure.

    Loop through the array item by item.
    add another loop inside the first that will loop through the second array with each item in the first array to see if it exists in the new array. If it does update that value in the new array. If it does not then add the item to the new array. when your outter loop is completed you will have a new array with no dupes and an accurate count for each item.

    If you do not need to store each session seperately then I would simply read the file into an array at the start of the program, update that array with each click and then over write the file with the updated values which would contain no dupes.
    Last edited by DataMiser; March 25th, 2009 at 01:06 PM.

  10. #10
    Join Date
    Feb 2009
    Posts
    252

    Re: [2005] Array Duplicates

    How about try this its a function, i made to get rid of duplicates of a simple way all it does its recives 2 parameters the combobox and the string to add on it. if the function returns true the that means that string its actually on the combobox list., in this case it wont try to add it, if false ill add it.

    of this simple way u wont have duplicates on ur combobox!.



    Code:
        Function Duplicated(ByVal Item As String, ByVal CList As ComboBox) As Boolean
            Dim c As Integer
            For c = 0 To CList.Items.Count - 1
                If Item = CList.Items.Item(c) Then
                    Return True
                    Exit For
                End If
            Next c
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Duplicated(TextBox1.Text, ComboBox1) = False Then
                ComboBox1.Items.Add(TextBox1.Text)
            End If
        End Sub
    Hoppe it helps you

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2005] Array Duplicates

    Really should create a class with two variables, mCLASS and mRANK

    Then, in your click event, search thru the list for the class. If found, add 1, if not call NEW and add it to the list with the mRANK of 1

    Then you will be able to get direct answers.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured