asp-classic - 将数字范围转换为逗号分隔列表(经典 ASP)
问题描述
似乎是一个非常常见的问题,但还没有找到一个经典的 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 等,但没有找到适用于 Classic ASP 的方法。任何人都可以帮忙吗?
仅供参考,永远不会有任何重复的数字,每个数字都是唯一的。
解决方案
执行此操作的基本步骤是
- 用逗号分隔您的初始列表
- 遍历每个项目,检查是否有连字符
- 如果有连字符,则从头到尾循环,并将该值添加到数组中,如果没有连字符,则只需添加该值
此时,您拥有所有值的列表,未排序且不唯一。
在 Classic ASP 中,您可以使用 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>
推荐阅读
- shiny - 异步:点击 actionButton 时显示进度并禁用同一用户的其他操作但允许并发用户
- python - 替换文件中的行的代码是重复行
- powershell - 在 Kotlin 中运行 powershell 脚本而不创建文件
- r - 蒙特卡罗模拟错误,不会更新计数
- identityserver3 - 提供的证书对加密/解密无效
- excel - 如何对 Excel 中带有“k”或“m”缩写的图形进行逆向工程
- php - 在档案页面上隐藏自定义帖子类型
- python - (Python)从二叉树交替输入?
- java - 为什么不使用按钮时,新活动不从此 OnClick 方法开始?
- html - CSS 页眉 - 正文 - 页脚打印在单独的页面上