2012-04-14 64 views
0

我基本上是從另一個論壇上粘貼它,我發現它被問到(沒有人回答)。它本質上是完全相同的事情即時嘗試去做:在一個數組中計數字符串的出現

在ASP經典,有沒有一種方法來計算一個字符串出現在一個字符串數組的次數,並根據字符串和出現次數輸出它們?

例如,如果我具有包含以下的數組:
你好
快樂
你好
你好
測試
你好
測試
快樂

輸出將是:

你好4
快樂2
測試1個
測試1

謝謝!

回答

1

我假設語言是VBScript(因爲這是大多數人使用的經典ASP)。

可以使用Dictionary對象,以保持個人計數的軌跡:

Function CountValues(pArray) 
    Dim i, item 
    Dim dictCounts 
    Set dictCounts = Server.CreateObject("Scripting.Dictionary") 

    For i = LBound(pArray) To UBound(pArray) 
     item = pArray(i) 
     If Not dictCounts.Exists(item) Then 
      dictCounts.Add item, 0 
     End If 
     dictCounts.Item(item) = dictCounts.Item(item) + 1 
    Next 

    Set CountValues = dictCounts 
End Function 
+0

謝謝!這正是我所需要的! – 2012-04-14 23:03:56

相關問題