Monday, July 13, 2009

I need to count the different type of characters in a string in ASP?

EX:





If I Have (,15,54,34,18,15,34) It needs to give me


15=2


54=1


34=2


18=1


etc.

I need to count the different type of characters in a string in ASP?
There are no hashes in VBScript. Also, it's not enough to split the string, you need to count the items, too. If you don't know what you are talking about, don't answer, please.





Assuming you mean classic ASP with VBScript, the solution is to split the string, then add the items to a dictionary -- VBScript's replacement for hashes -- to get your results.





%26lt;%


Dim strInput = 15,54,34,18,15,34


Dim arrInput = Split(strInput, ",")





Dim objDict = Server.CreateObject( "Scripting.Dictionary" )





Dim Thing


Dim I





'populate dictionary


For Each Thing In arrInput


If Not objDict.Exists(Thing)


objDict.Add Thing, 0


End If


Next





'do counts


For Each Thing In arrInput


objDict.Item(Thing) = objDict.Item(Thing) + 1


Next





'Display results


Dim ItemsArray = objDict.Items


Dim KeysArray = objDict.Keys





For I = LBound(ItemsArray) To UBound(ItemsArray)


Response.Write KeysArray(I) %26amp; ": " %26amp; ItemsArray(I) %26amp; "
"


Next


%%26gt;
Reply:are those characters divided by comma ?
Reply:The best most general solution is to make a hashmap where the value returned from the hashmap for a character is the number of that character you found.
Reply:Use split().


Split(foo,bar) will return an array of all of the substrings in foo divided by bar.


No comments:

Post a Comment