Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting occurences....?

Status
Not open for further replies.

wraygun

Programmer
Dec 9, 2001
272
US
I'm just goofing around, attempting to further my knowledge of VB and I was wondering if someone could give me some pointers or direction on how to approach writing a simple program to count the occurences of each numeral in a random typing session in a textbox. For example:

Text1="1239821938127"

Output would be something like:

CountedNumerals(0)=0
CountedNumerals(1)=3
CountedNumerals(2)=2
CountedNumerals(3)=2
CountedNumerals(4)=0

etc.....

Any Input will be greatly appreciated.

Thanks,
Harold
***You can't change your past, but you can change your future***
 

Since you are only dealing with numbers something like this could work for you.
[tt]
Option Explicit

Dim I as Integer, MyArray(9) As Integer

Private Sub CountNumbersInString(S as String)

For I = 1 To Len(S)
MyArray(Val(Mid(S, I, 1))) = MyArray(Val(Mid(S, I, 1))) + 1
Next I

End Sub
[/tt]

As you can see there is no error checking in this sub and especially exception handling, i.e. if any of the characters in the string are not 0-9 then the value of MyArray(0) will be increased by 1 unnecessarily. You could put a check in the loop with the isnumeric function.

Good Luck
 
Since you're objective is to further your knowledge, I would suggest that look into Regular Expressions for this problem. RE can be quite powerful in a number of situations, this case being one.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Hi all,

Thanks for your replies. The solution I came up with is as follows. Please feel free to comment.


Code:
Dim reg As New RegExp
Dim strTest As String
Dim regPattern As String
Dim Matches As MatchCollection
Dim mtch As Match
Dim Cnt As Integer
Dim Counter As Integer
Dim blnFound As Boolean
Dim DispNumerals(10) As Integer

Private Sub Command1_Click()
CountNumerals
End Sub

Public Sub CountNumerals()
strTest = Text1.Text
Text2 = ""

Cnt = 0
For Counter = 0 To 9
regPattern = Counter
With reg
    .Pattern = regPattern
    reg.Global = True
   
   Set Matches = .Execute(strTest)
    
    For Each mtch In Matches
    Cnt = Cnt + 1
       Next mtch

DispNumerals(Counter) = Cnt
Cnt = 0

End With

Set reg = Nothing
Next

For Counter = 0 To 9
Text2 = Text2 & "Numeral " & Counter & ": " & DispNumerals(Counter) & vbCrLf
Next
End Sub
***You can't change your past, but you can change your future***
 
a MatchCollection object has a .Count property

Set Matches = .Execute(strTest)

' For Each mtch In Matches
' Cnt = Cnt + 1
' Next mtch

DispNumerals(Counter) = Matches.Count
 
Why not use the Split Function (VB6):-

StringOccurs = UBound(Split("1239821938127", "1"))


Codefish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top