Asp classic 将数字范围转换为逗号分隔列表(经典ASP)

Asp classic 将数字范围转换为逗号分隔列表(经典ASP),asp-classic,Asp Classic,这似乎是一个非常常见的问题,但尚未找到一个经典的ASP示例 我从我们继承的数据库中获得了如下数据: 120-128,10,20,30,12-19 我需要能够将其转换为一个逗号分隔的列表,以连续的顺序,不仅提取当前的数字,而且提取范围内的数字(由-) 因此,在上面的示例中,我希望输出: 10,12,13,14,15,16,17,18,19,20, 然后我希望能够将结果存储为单个变量,以便以后可以对其进行更多的处理 我找到了Python方法、C#、Javascript、PHP等,但没有找到用于经典A

这似乎是一个非常常见的问题,但尚未找到一个经典的ASP示例

我从我们继承的数据库中获得了如下数据:

120-128,10,20,30,12-19

我需要能够将其转换为一个逗号分隔的列表,以连续的顺序,不仅提取当前的数字,而且提取范围内的数字(由-)

因此,在上面的示例中,我希望输出:

10,12,13,14,15,16,17,18,19,20,

然后我希望能够将结果存储为单个变量,以便以后可以对其进行更多的处理

我找到了Python方法、C#、Javascript、PHP等,但没有找到用于经典ASP的方法。 有人能帮忙吗


仅供参考,不会有任何重复的数字,每个数字都是唯一的。

实现这一点的基本步骤是

  • 用逗号拆分初始列表
  • 遍历这些项中的每一项,检查是否有连字符
  • 如果有连字符,则从头到尾循环,并将该值添加到数组中;如果没有连字符,则仅添加该值
  • 此时,您将有一个所有值的列表,未排序且不唯一

    在经典ASP中,您可以使用Arraylist来帮助排序和唯一性。创建两个arraylist对象。一个将包含非唯一列表,然后另一个将包含最终的唯一列表

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <body>
        <p>
            <%
    
        v="120-128,10,20,30,12-19,13-22" 'our original string to parse
    
        set uniqueList=CreateObject("System.Collections.ArrayList") 'final unique list
        set mynumbers=CreateObject("System.Collections.ArrayList")  'a working list
    
        'first split the values by the comma
        splitCom=Split(v, ",")
    
        'now go through each item
        for itemnumber=0 to ubound(splitCom)
            itemToAdd=splitCom(itemnumber)
           
            if InStr(itemToAdd, "-")>0 then  'if the item has a hyphen, then we have a range of numbers
                rangeSplit=Split(itemToAdd, "-")
    
                for itemToAdd=rangeSplit(0) to rangeSplit(1)
                    mynumbers.Add CInt(itemToAdd)
                next
            else
                mynumbers.Add Cint(itemToAdd) 'otherwise add the value itself
            end if
        next
    
        'at this point, mynumbers contains a full list of all your values, unsorted, and non-unique.
    
        mynumbers.sort  'sort the list. Can't be any easier than this
    
        'output the non-unique list, and build a unique list while we are at it.
        Response.Write("Non-unique list<br />")
    
        for each item in mynumbers                      'iterate through each item
            Response.Write(item & "<br />")             'print it
                if (not uniqueList.Contains(item)) then 'is the value in our unique list?
                    uniqueList.Add(item)                'no, so add it to the unique list
                end if
        next
    
        'now output the unique list.
        Response.Write("<br />Unique list<br />")
        for each item in uniqueList
            Response.Write(item & "<br />")
        next
            %>
        </p>
    </body>
    </html>
    
    
    
    


    能否发布您的Javascript解决方案。在经典ASP中,您可以使用JS作为服务器端语言,而不是VBS。这是一个相当不错的面试小问题,涉及字符串操作、数组、排序和重复数据消除。有兴趣看看你的JS代码。正如John所说,您可能无论如何都可以在服务器上运行JS,因此可能不需要纯ASP解决方案。@John Classic ASP支持包含JScript而非JavaScript的活动脚本语言。当您将JavaScript指定为服务器端语言时,您实际上使用的是基于ECMAScript 3规范的JScript,因此不要将其与现代JavaScript混淆,后者目前至少是ECMAScript 6,ECMAScript 2020今年发布,因此它在不断发展,与JScript不同的是,JScript是ES3,不会改变。JS与JScript的对比很好。即使是JScript也具备满足OP需求的基本能力,不过?@VanquishedWombat绝对如此,只是想指出它们不应该被一视同仁。