首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数字范围转换为逗号分隔列表(经典ASP)

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

Stack Overflow用户
提问于 2020-11-10 14:25:10
回答 1查看 133关注 0票数 0

似乎是一个非常常见的问题,但还没有找到一个经典ASP的例子。

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

120-128 10 20 30 12-19

我需要能够将其转换为连续顺序的逗号分隔列表,不仅要拉出当前的数字,而且要在范围内(由-)指定数字。

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

10,12,13,14,15,16,17,18,19,20,30,120,121,122,123,124,125,126,127,128

然后,我希望能够将结果存储为一个单一变量,这样以后我可以使用它做更多的工作。

我已经找到了Python,C#,Javascript,PHP等,但是没有一个用于经典ASP。有人能帮忙吗?

FYI,不会有任何重复的数字,每个数字将是唯一的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-18 02:46:44

这样做的基本步骤是

  1. 通过逗号
  2. 将初始列表拆分,遍历每一项,检查是否存在连字符
  3. (如果有连字符),然后从开始循环到结尾,然后将该值添加到数组中,如果没有连字符,则只需添加值

即可。

此时,您有一个所有值的列表,这些值都是未排序的,而不是唯一的。

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

代码语言:javascript
复制
<!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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64770769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档