-1

I am new to writing in asp and vb and am stuck on a piece of logic where I need to retrieve data from a web form, count the number of entries and then order them alphanumerically.

I have a webform with the multiple text boxes that can be filled out and submitted that looks a little like this: (excuse the spreadsheet, it's a visual aid only)

enter image description here

I have made an array that contains their values like this:

myArray = array(town, medal, record, sport)

I would like to count and order (everything alphanumerically) the total medals, how many of each medal each town won and the number of records set by each town.

My psuedocode looks a little like this, hopefully I am a little on the right track in terms of logic. The main area I am a little short in is knowing what statements would be good and where, especially to order them alphanumerically.

'this is the psuedocode for the total medals per town
tally = 0 'Set tally to 0
for myArray(town) 'For each town
    for myArray(medal) 'For each medal
        tally = tally + 1 'Add 1 to the total tally
        response.write(myArray(town) "has" tally "medals" & "<br>")
    next
next

'this is the pseudocode for the individual medals
for myArray(town) 'For each town
    for myArray(medal) 'For each medal
        goldTally = 0
        silverTally = 0
        bronzeTally = 0
        if medal = "G"
            goldTally = goldTally + 1
        elseif medal = "S"
            silverTally = silverTally + 1
        else medal = "B"
            bronzeTally = bronzeTally + 1
        response.write(myArray(town) "has:" goldTally "gold medals" &"<br>"
                                         silverTally "silver medals" &"<br>"
                                         bronzeTally "bronze medals" &"<br>" 
    next
next               

Any help you can give would be greatly appreciated thanks heaps.

FactorV
  • 45
  • 4

1 Answers1

2

The VBScript tool for counting/grouping/classifying is the Dictionary. Some use cases: Set ops, word list, split file.

Simple Arrays can be sorted via using an ArrayList. [Array vs Arraylist], fancy sorting7.

For tabular data, use a disconnected recordset.

Inline demo:

Option Explicit

' simple sample data
Dim a : a = Split("b c a b b c a a b")

' use a dictionary for counting/grouping
Dim d : Set d = CreateObject("Scripting.Dictionary")
Dim e
For Each e In a
    d(e) = d(e) + 1
Next
WScript.Echo Join(d.Keys)
WScript.Echo Join(d.Items)

' use an ArrayList for sorting simple arrays
Dim l : Set l = CreateObject("System.Collections.ArrayList")
For Each e in a
    l.Add e
Next
l.Sort
WScript.Echo Join(l.ToArray())

' use a disconnected recordset for tabular data
Const adVarChar    = 200
Const adInteger    = 2
Const adClipString = 2
Dim r : Set r = CreateObject("ADODB.Recordset")
r.Fields.Append "k", adVarChar, 50
r.Fields.Append "n", adInteger
r.Open
For Each e In d.Keys
    r.AddNew
    r.Fields("k").value = e
    r.Fields("n").value = d(e)
    r.Update
Next
r.MoveFirst
Do Until r.EOF
   WScript.Echo r.Fields("k").value, r.Fields("n").value
   r.MoveNext
Loop
r.Sort = "k DESC"
WScript.Echo r.GetString(adClipString, , ", ", "; ", "null")

output:

cscript 39305170.vbs
b c a
4 2 3
a a a b b b b c c
a 3
c, 2; b, 4; a, 3;

BTW: Even in a pseudo code language,

for myArray(town) 'For each town

and

response.write(myArray(town) "has:" goldTally "gold medals" ...

can't work at the same time.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96