|
-
Mar 24th, 2009, 05:48 AM
#1
Thread Starter
Hyperactive Member
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?
-
Mar 25th, 2009, 12:37 AM
#2
Thread Starter
Hyperactive Member
Re: Array Duplicates
Can anyone help with an idea?
-
Mar 25th, 2009, 02:36 AM
#3
Re: Array Duplicates
If you use return inside your loop the loop will stop as the return is executed.
you need to add a boolean flag to indicate if duplicates where found... something like
Code:
Private Function SSOrganiseList(ByVal arr As Array) As Boolean
Dim SSDup(arr.Length - 1)
DIm Found as Boolean
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) = i
Found = True
End If
End If
Next
Return Found
End Function
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
-
Mar 25th, 2009, 06:47 AM
#4
Thread Starter
Hyperactive Member
Re: Array Duplicates
OK, thanx Dnereb.
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.
Last edited by GrimmReaper; Mar 25th, 2009 at 10:18 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|