views:

145

answers:

3

I have an array in ASP that looks like this:

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

What I want to do is group them together with a count so I would have:

Number  Count
2       1
3       3
5       1
7       2

Is this possible? If so how?

A: 

This is an example in 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;
}

Haven't tested it. But should work, or at least give you the idea.

Eibx
he is asking for asp-classic ..
Gaby
Yes, but should still give Tonyyeb an idea on how to create it.
Eibx
true .. just making sure :)
Gaby
+3  A: 

In asp-classic there are no associative arrays ..

The alternative is the Scripting.Dictionary

so

<%
    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
%>

this has created what you want ... to see it now

<%
    for each i in dictArray
        response.write( i & " : " & dictArray(i) & "<br />")
    next
 %>
Gaby
Worked perfectly! Thanks very much.
tonyyeb
A: 

You need to use a two-dimensional array in VBScript; http://www.4guysfromrolla.com/webtech/041101-1.2.shtml which will be the representation of Dictionary<int, int> that @Eibx suggested.

Dave Anderson