Results 1 to 4 of 4

Thread: [RESOLVED] Problem counting words

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [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.

  2. #2
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    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:
    1. Public Function WORD_COUNT(ByVal StringToCount As String) As String
    2. On Error GoTo ErrorHandler
    3.  
    4. Dim WordsToCount As Variant
    5. Dim intIndex As Integer
    6. Dim ScanCount As Integer
    7. Dim CurrentWord As String
    8. Dim WordCount As Integer
    9. Dim FinishedText As String
    10.  
    11. WordCount = 0
    12.  
    13. WordsToCount = Split(StringToCount, Chr(32))
    14.  
    15. For intIndex = 0 To UBound(WordsToCount)
    16.     CurrentWord = WordsToCount(intIndex)
    17.     WordCount = 1
    18.    
    19.     For ScanCount = 0 To UBound(WordsToCount)
    20.         If ScanCount <> intIndex Then
    21.             If WordsToCount(ScanCount) = CurrentWord Then
    22.                 WordCount = WordCount + 1
    23.             End If
    24.         End If
    25.     Next
    26.    
    27.     [B]If InStrRev(FinishedText, CurrentWord & "(" & WordCount & ")") = 0 Then
    28.       FinishedText = FinishedText & CurrentWord & "(" & WordCount & ")" & vbCrLf
    29.     End If[/B]
    30.        
    31. Next
    32.  
    33. WORD_COUNT = FinishedText
    34.  
    35. Exit Function
    36. ErrorHandler:
    37. ErrorHandler.ErrorAlert Err.Number, Err.Description, "clsPageParcer.WORD_COUNT"
    38. End Function
    Hope this helps.
    Give your music collection a whole new life with PartyTime Jukebox

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: [RESOLVED] Problem counting words

    Yes, that is a good idea that works very well. Thank you.

  4. #4
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: [RESOLVED] Problem counting words

    np, glad to help
    Give your music collection a whole new life with PartyTime Jukebox

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