2010-01-26 55 views
2

我在ASP的數組,看起來像這樣:在經典的asp中分組數組?

3,5,7,7,3,2,3 

我想要做一個計數它們組合在一起,所以我將有:

Number Count 
2  1 
3  3 
5  1 
7  2 

這可能嗎?如果是這樣如何?

回答

7

在ASP經典有沒有關聯數組..

另一種方法是Scripting.Dictionary

所以

<% 
    dim ar 
    ar = array(3,5,7,7,3,2,3) 

    dim dictArray 

    set dictArray = server.CreateObject("Scripting.Dictionary") 

    for each i in ar 
     if dictArray.exists(i) then 
      dictArray(i) = dictArray(i) + 1 
     else 
      dictArray(i) = 1 
     end if 
    next 
%> 

這已經創造了你想要的東西......看現在

<% 
    for each i in dictArray 
     response.write(i & " : " & dictArray(i) & "<br />") 
    next 
%> 
+0

完美地工作!非常感謝。 – tonyyeb 2010-01-26 13:26:32

0

這是在C#

public Dictionary<int, int> SortList(string text) 
{ 
    Dictionary<int, int> sortedArray = new Dictionary<int, int>(); 
    List<int> array = new List<int>() { 1, 2, 2, 2, 5, 4 }; 

    for (int i = 0; i < array.Count; i++) 
    { 
     if (DoesExsist(sortedArray, array[i])) 
     { 
      sortedArray[array[i]]++; 
     } 
     else 
     { 
      sortedArray.Add(array[i], 1); 
     } 
    } 

    return sortedArray; 
} 

private bool DoesExsist(Dictionary<int, int> array, int keyvalue) 
{ 
    foreach (KeyValuePair<int, int> item in array) 
    { 
     if (item.Key == keyvalue) 
     { 
      return true; 
     } 
    } 

    return false; 
} 

一個例子沒有測試它。但應該工作,或至少給你的想法。

+3

他是要求asp-classic .. – 2010-01-26 12:51:16

+0

是的,但是仍然應該給Tonyyeb一個關於如何創建它的想法。 – 2010-01-26 13:07:09

+0

true ..只要確保:) – 2010-01-26 13:16:53