【问题标题】:Classic ASP count occurrences in array and output the results数组中的经典 ASP 计数并输出结果
【发布时间】:2015-09-17 18:16:09
【问题描述】:

我创建了一个字符串,需要对其进行简化,以便将数组中的正确信息传递到下一页。 date1 - date3 是实际日期,但为了简单起见,我只输入了 date1 等。

string= " date1,date2,date1,date3,date1,date2"
Array = split(string,",")

我需要以下输出/组织:

3 date1
2 date2
1 date3 

这样我可以传递信息

3,2015-09-09$2,2015-09-20$1,2015-09-25

【问题讨论】:

    标签: arrays forms asp-classic


    【解决方案1】:

    您可能希望使用字典并将每个日期字符串存储为键。键的可以是计数(日期出现的次数)。

    例如:

    ' Split dates into an array...
    Dim a
    a = Split("2015-09-09,2015-09-20,2015-09-09,2015-09-09,2015-09-20,2015-09-25", ",")
    
    ' Store each date into a dictionary and count the occurrences...
    Dim d, dt
    Set d = Server.CreateObject("Scripting.Dictionary")
    For Each dt In a
        If d.Exists(dt) Then d(dt) = d(dt) + 1 Else d.Add dt, 1
    Next
    
    ' Concatenate dictionary items...
    Dim k, s
    For Each k In d.Keys
        If Len(s) > 0 Then s = s & "$"
        s = s & d(k) & "," & k
    Next
    
    Response.Write s
    

    输出:

    3,2015-09-09$2,2015-09-20$1,2015-09-25
    

    【讨论】:

    • 非常感谢。我有正确的想法,并关闭。 :) 我真的很感激。
    • 乐于助人。如果您觉得答案有用,请考虑upvoting and/or accepting
    猜你喜欢
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多