Counting instance of number in string or array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • movieking81
    New Member
    • Feb 2007
    • 52

    Counting instance of number in string or array

    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,98 8,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
  • multinett
    New Member
    • Sep 2011
    • 16

    #2
    i think you have to write a small subroutine like this:
    Code:
    <%
    mystring= "4,7,12,54,87,988,3456,76,21,12,012,122,76,5,12,9,3,87,38,7"
    myCompare="12"
    
    
    response.write myCompare &" wurde "&countPattern(mystring,mycompare)&" gefunden"
    
    
    
    function countPattern(sString,sCompare)
    iCount=0
    while instr(","&sString&",", ","&sCompare&",") > 0
    	iCount=iCount+1
    	myString=replace(sString,","&sCompare&",",",",1,1,1)
    wend
    countPattern=iCount
    end function
    
    %>
    maybe it works also with regexpression but i don't know exactly

    Comment

    • multinett
      New Member
      • Sep 2011
      • 16

      #3
      If you want a result for all numbers, you should create a temprary database:

      Code:
      <%
      mystring= "4,7,12,54,87,988,3456,76,21,76,5,9,3,87,38,7"
      aString=split(mystring,",")
      
      
      
      ' Definition der ADO Datentypen (DataTypeEnum)
      Const adBinary = 128    ' Daten
      Const adBoolean = 11    ' Boolean (True/False)
      Const adChar = 129      ' Text
      Const adCurrency = 6    ' Waehrung
      Const adDate = 7        ' Datum
      Const adDecimal = 14    ' Exakte Dezimalzahl
      Const adDouble = 5      ' Fliesskommazahl hoher Genauigkeit
      Const adInteger = 3     ' Ganze Zahl
      Const adSingle = 4      ' Fliesskommazahl niedriger Genauigkeit
      
      ' Erstellen des Recordset Objektes
      	Set rs = CreateObject("ADODB.Recordset")
      	'    Definition der Datenstruktur des Recordsets
      	'    rs.Fields.Append Name, Datentype, Laenge, Attribute
      
      rs.Fields.Append "myValue", adInteger 'create a recordset-Field
      rs.open
      for i=0 to ubound(aString)
      	rs.addNew
      	rs("myValue")=aString(i):rs.update  'insert the data into the recordset
      next
      
      
      rs.moveFirst
      rs.sort = "myValue"
      iTmp=rs("myvalue")
      iCount=0
      do while not rs.eof
      	if rs("myValue") <> iTmp then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >":iCount=0
      	iCount=iCount+1
      	iTmp=rs("myValue")
      	rs.moveNext
      	
      loop
      rs.close
      
      
      %>
      if someone has an idea how to group the recordset and ask for (select count(*) ...)
      - he should post it here..

      Comment

      • multinett
        New Member
        • Sep 2011
        • 16

        #4
        SORRY!
        a small mistake! i forgot the last number:

        try this (this is correct)
        Code:
        rs.moveFirst
        rs.sort = "myValue"
        iTmp=rs("myvalue")
        iCount=0
        do while not rs.eof
        	bLoop=true
        	if rs("myValue") <> iTmp then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >":iCount=0
        	iCount=iCount+1
        	iTmp=rs("myValue")
        	rs.moveNext
        	bLoop=false
        	
        loop
        if not bLoop then response.write "die Zahl "&iTmp&" wurde "&iCount&" x gefunden<br >"
        rs.close

        Comment

        • movieking81
          New Member
          • Feb 2007
          • 52

          #5
          Great, I'll get working on this. Thanks for the help.

          Comment

          Working...