【问题标题】:ASP/VB: Counting and ordering array valuesASP/VB:对数组值进行计数和排序
【发布时间】:2016-09-03 09:04:51
【问题描述】:

我是用 asp 和 vb 编写的新手,我被困在一个逻辑上,我需要从 web 表单中检索数据,计算条目数,然后按字母数字顺序排列。

我有一个带有多个可以填写和提交的文本框的网络表单,看起来有点像这样:(请原谅电子表格,它只是一个视觉辅助)

我已经创建了一个包含它们的值的数组,如下所示:

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

我想统计和排序(按字母数字顺序)总奖牌、每个城镇赢得的奖牌数量以及每个城镇创造的记录数量。

我的伪代码看起来有点像这样,希望我在逻辑方面有点走上正轨。我不太了解的主要领域是知道哪些语句是好的以及在哪里,尤其是按字母数字顺序排列它们。

'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               

如果您能提供任何帮助,我们将不胜感激。

【问题讨论】:

    标签: asp.net vbscript


    【解决方案1】:

    用于计数/分组/分类的 VBScript 工具是Dictionary。一些用例:Set opsword listsplit file

    可以使用ArrayList 对简单数组进行排序。 [数组与数组列表],fancy sorting7

    对于表格数据,请使用断开连接的recordset

    内联演示:

    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")
    

    输出:

    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;
    

    顺便说一句:即使是伪代码语言,

    for myArray(town) 'For each town
    

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

    不能同时工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2011-03-17
      • 1970-01-01
      相关资源
      最近更新 更多