By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,162 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,162 IT Pros & Developers. It's quick & easy.

Counting instance of number in string or array

P: 52
Hello all -

Does anyone here have any suggestions how I start to count the instances of a particular number with a string or array? I think I have an idea, but I'm not really sure. I have the following:

string - 4,7,12,54,87,988,3456,76,21,76,5,9,3,87,38,7

From that string I need to count how many times the 7 occurs, how many times 87 occurs, etc. I can use whatever delimiter is necessary. Any ideas?

Thanks
Oct 16 '11 #1
Share this Question
Share on Google+
4 Replies


P: 16
i think you have to write a small subroutine like this:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. mystring= "4,7,12,54,87,988,3456,76,21,12,012,122,76,5,12,9,3,87,38,7"
  3. myCompare="12"
  4.  
  5.  
  6. response.write myCompare &" wurde "&countPattern(mystring,mycompare)&" gefunden"
  7.  
  8.  
  9.  
  10. function countPattern(sString,sCompare)
  11. iCount=0
  12. while instr(","&sString&",", ","&sCompare&",") > 0
  13.     iCount=iCount+1
  14.     myString=replace(sString,","&sCompare&",",",",1,1,1)
  15. wend
  16. countPattern=iCount
  17. end function
  18.  
  19. %>
  20.  
maybe it works also with regexpression but i don't know exactly
Oct 19 '11 #2

P: 16
If you want a result for all numbers, you should create a temprary database:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3. mystring= "4,7,12,54,87,988,3456,76,21,76,5,9,3,87,38,7"
  4. aString=split(mystring,",")
  5.  
  6.  
  7.  
  8. ' Definition der ADO Datentypen (DataTypeEnum)
  9. Const adBinary = 128    ' Daten
  10. Const adBoolean = 11    ' Boolean (True/False)
  11. Const adChar = 129      ' Text
  12. Const adCurrency = 6    ' Waehrung
  13. Const adDate = 7        ' Datum
  14. Const adDecimal = 14    ' Exakte Dezimalzahl
  15. Const adDouble = 5      ' Fliesskommazahl hoher Genauigkeit
  16. Const adInteger = 3     ' Ganze Zahl
  17. Const adSingle = 4      ' Fliesskommazahl niedriger Genauigkeit
  18.  
  19. ' Erstellen des Recordset Objektes
  20.     Set rs = CreateObject("ADODB.Recordset")
  21.     '    Definition der Datenstruktur des Recordsets
  22.     '    rs.Fields.Append Name, Datentype, Laenge, Attribute
  23.  
  24. rs.Fields.Append "myValue", adInteger 'create a recordset-Field
  25. rs.open
  26. for i=0 to ubound(aString)
  27.     rs.addNew
  28.     rs("myValue")=aString(i):rs.update  'insert the data into the recordset
  29. next
  30.  
  31.  
  32. rs.moveFirst
  33. rs.sort = "myValue"
  34. iTmp=rs("myvalue")
  35. iCount=0
  36. do while not rs.eof
  37.     if rs("myValue") <> iTmp then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >":iCount=0
  38.     iCount=iCount+1
  39.     iTmp=rs("myValue")
  40.     rs.moveNext
  41.  
  42. loop
  43. rs.close
  44.  
  45.  
  46. %>
  47.  
if someone has an idea how to group the recordset and ask for (select count(*) ...)
- he should post it here..
Oct 19 '11 #3

P: 16
SORRY!
a small mistake! i forgot the last number:

try this (this is correct)
Expand|Select|Wrap|Line Numbers
  1. rs.moveFirst
  2. rs.sort = "myValue"
  3. iTmp=rs("myvalue")
  4. iCount=0
  5. do while not rs.eof
  6.     bLoop=true
  7.     if rs("myValue") <> iTmp then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >":iCount=0
  8.     iCount=iCount+1
  9.     iTmp=rs("myValue")
  10.     rs.moveNext
  11.     bLoop=false
  12.  
  13. loop
  14. if not bLoop then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >"
  15. rs.close
  16.  
Oct 19 '11 #4

P: 52
Great, I'll get working on this. Thanks for the help.
Oct 22 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.