Asp classic 数组中字符串出现次数计数的进一步问题

Asp classic 数组中字符串出现次数计数的进一步问题,asp-classic,vbscript,Asp Classic,Vbscript,我从其他地方抄袭了一个问题和答案,因为它部分进入了我需要的内容,但不是全部 在ASP classic中,是否有方法计算字符串在字符串数组中出现的次数,并根据字符串和出现次数输出它们 例如,如果我有一个包含以下内容的数组: hello happy hello hello testing hello test happy 这很好,但我不知道如何抓取前两个最常用的单词,显示它们,并将它们放在自己的变量中供其他地方使用 有人可以帮忙吗?您可以使用循环遍历dictionary对象。在该循环中,在一个新数组

我从其他地方抄袭了一个问题和答案,因为它部分进入了我需要的内容,但不是全部

在ASP classic中,是否有方法计算字符串在字符串数组中出现的次数,并根据字符串和出现次数输出它们

例如,如果我有一个包含以下内容的数组:

hello happy hello hello testing hello test happy 这很好,但我不知道如何抓取前两个最常用的单词,显示它们,并将它们放在自己的变量中供其他地方使用


有人可以帮忙吗?

您可以使用循环遍历dictionary对象。在该循环中,在一个新数组或两个新变量中跟踪最上面的两个键及其计数。

不能在VBScript中对字典对象进行排序,因此必须使用其他方法

我的建议是使用一个断开连接的记录集对象来保存项目及其引用。这样的对象本机支持排序,并且非常容易使用。要实现这一点,请使用以下功能:

Function CountValues_Recordset(pArray)
    Dim i, item
    Dim oRS
    Const adVarChar = 200
    Const adInteger = 3
    Set oRS = CreateObject("ADODB.Recordset")
    oRS.Fields.Append "Item", adVarChar, 255
    oRS.Fields.Append "Occurrences", adInteger, 255
    oRS.Open
    For i = LBound(pArray) To UBound(pArray)
        item = pArray(i)
        oRS.Filter = "Item='" & Replace(item, "'", "''") & "'"
        If (oRS.EOF) Then
            oRS.AddNew
            oRS.Fields("Item").Value = item
            oRS.Fields("Occurrences").Value = 1
        Else  
            oRS.Fields("Occurrences").Value = oRS.Fields("Occurrences").Value + 1
        End If
        oRS.Update
        oRS.Filter = ""
    Next
    oRS.Sort = "Occurrences DESC"
    oRS.MoveFirst
    Set CountValues_Recordset = oRS
End Function
并使用它实现您想要的输出:

Dim myArray, oRS
myArray = Array("happy", "hello", "hello", "testing", "hello", "test", "hello", "happy")
Set oRS = CountValues_Recordset(myArray)
Do Until oRS.EOF
    Response.Write(oRS("item") & " " & oRS("Occurrences") & "<br />")
    oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
Dim myArray,oRS
myArray=Array(“happy”、“hello”、“hello”、“testing”、“hello”、“test”、“hello”、“happy”)
设置oRS=CountValues\u记录集(myArray)
直到oRS.EOF为止
响应。写入(oRS(“项目”)和“&oRS(“事件”)和“
”) 下一个 环 好的,结束 设置oRS=无
使用记录集后,不要忘记关闭并处理它

Function CountValues_Recordset(pArray)
    Dim i, item
    Dim oRS
    Const adVarChar = 200
    Const adInteger = 3
    Set oRS = CreateObject("ADODB.Recordset")
    oRS.Fields.Append "Item", adVarChar, 255
    oRS.Fields.Append "Occurrences", adInteger, 255
    oRS.Open
    For i = LBound(pArray) To UBound(pArray)
        item = pArray(i)
        oRS.Filter = "Item='" & Replace(item, "'", "''") & "'"
        If (oRS.EOF) Then
            oRS.AddNew
            oRS.Fields("Item").Value = item
            oRS.Fields("Occurrences").Value = 1
        Else  
            oRS.Fields("Occurrences").Value = oRS.Fields("Occurrences").Value + 1
        End If
        oRS.Update
        oRS.Filter = ""
    Next
    oRS.Sort = "Occurrences DESC"
    oRS.MoveFirst
    Set CountValues_Recordset = oRS
End Function
Dim myArray, oRS
myArray = Array("happy", "hello", "hello", "testing", "hello", "test", "hello", "happy")
Set oRS = CountValues_Recordset(myArray)
Do Until oRS.EOF
    Response.Write(oRS("item") & " " & oRS("Occurrences") & "<br />")
    oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing