|
-
May 2nd, 2006, 05:56 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Problem counting words
I have put some code in an array and I would like to create a list of each word, and how many times it appears in the array. I can parse thru the code, but I am having duplication. Everything I have thought of has to do with going thru the list over and over, that obviously isn't productive. Below is my code. I would be very greatful if someone could help me find an efficient way of doing this. The code should put the "word (count)" on each line, which I can format.
Thank you for the help.
Code:
Public Function WORD_COUNT(ByVal StringToCount As String) As String
On Error GoTo ErrorHandler
Dim WordsToCount As Variant
Dim intIndex As Integer
Dim ScanCount As Integer
Dim CurrentWord As String
Dim WordCount As Integer
Dim FinishedText As String
WordCount = 0
WordsToCount = Split(StringToCount, Chr(32))
For intIndex = 0 To UBound(WordsToCount)
CurrentWord = WordsToCount(intIndex)
WordCount = 1
For ScanCount = 0 To UBound(WordsToCount)
If ScanCount <> intIndex Then
If WordsToCount(ScanCount) = CurrentWord Then
WordCount = WordCount + 1
End If
End If
Next
FinishedText = FinishedText & CurrentWord & "(" & WordCount & ")" & vbCrLf
Next
WORD_COUNT = FinishedText
Exit Function
ErrorHandler:
ErrorHandler.ErrorAlert Err.Number, Err.Description, "clsPageParcer.WORD_COUNT"
End Function
As you can see, the function returns the string, which will be placed in a rich text box.
-
May 2nd, 2006, 08:00 PM
#2
Fanatic Member
Re: Problem counting words
You could avoid duplicate by checking if your CurrentWord is already in your FinishedText variable.
If it is'nt then append to it.
Like...
VB Code:
Public Function WORD_COUNT(ByVal StringToCount As String) As String
On Error GoTo ErrorHandler
Dim WordsToCount As Variant
Dim intIndex As Integer
Dim ScanCount As Integer
Dim CurrentWord As String
Dim WordCount As Integer
Dim FinishedText As String
WordCount = 0
WordsToCount = Split(StringToCount, Chr(32))
For intIndex = 0 To UBound(WordsToCount)
CurrentWord = WordsToCount(intIndex)
WordCount = 1
For ScanCount = 0 To UBound(WordsToCount)
If ScanCount <> intIndex Then
If WordsToCount(ScanCount) = CurrentWord Then
WordCount = WordCount + 1
End If
End If
Next
[B]If InStrRev(FinishedText, CurrentWord & "(" & WordCount & ")") = 0 Then
FinishedText = FinishedText & CurrentWord & "(" & WordCount & ")" & vbCrLf
End If[/B]
Next
WORD_COUNT = FinishedText
Exit Function
ErrorHandler:
ErrorHandler.ErrorAlert Err.Number, Err.Description, "clsPageParcer.WORD_COUNT"
End Function
Hope this helps.
-
May 3rd, 2006, 11:27 AM
#3
Thread Starter
Hyperactive Member
Re: [RESOLVED] Problem counting words
Yes, that is a good idea that works very well. Thank you.
-
May 3rd, 2006, 06:32 PM
#4
Fanatic Member
Re: [RESOLVED] Problem counting words
np, glad to 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|