Click to See Complete Forum and Search --> : [2005] Array Duplicates


GrimmReaper
March 24th, 2009, 07:21 AM
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:
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:
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:
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:

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):
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?

dglienna
March 24th, 2009, 06:50 PM
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

GrimmReaper
March 25th, 2009, 12:39 AM
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?

dglienna
March 25th, 2009, 01:05 AM
You have only one loop

DataMiser
March 25th, 2009, 01:55 AM
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.

GrimmReaper
March 25th, 2009, 06:26 AM
OK, thanx DataMiser.
I have now replaced the Function with Sub.

I did this in there (it's basicall the same still):
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:
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? :confused:

Oblio
March 25th, 2009, 09:40 AM
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.

DataMiser
March 25th, 2009, 12:41 PM
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.

DataMiser
March 25th, 2009, 01:03 PM
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.

Alphadan
March 25th, 2009, 07:37 PM
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!.



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

dglienna
March 25th, 2009, 08:43 PM
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.