Grouping an array in classic asp / vbscript?

Soldato
Joined
31 May 2006
Posts
4,239
Location
127.0.0.1
I have an array in ASP that looks like this:

Code:
3,5,7,7,3,2,3

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

Code:
Number  Count
2       1
3       3
5       1
7       2

Is this possible? If so how? Using a temp table in SQL?
 
Thanks for that. That is what I would use to store the groupings perhaps but is there a way to group the original array entries into a multi-dimensional array?
 
You could use a Dictionary object (vbscript).

The Key could be your number in the array and give it an initial value of 1. Keep looping thru the array. If the Key already exists then just ADD 1 to the value, else create a new Key and assign the Value 1

Thats how I would do an in memory one.
 
I was given the answer on another forum:

Code:
<%
    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
 %>
 
I was given the answer on another forum:

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

Thats exactly what I told you in the my post above that :)
 
Back
Top Bottom