Seems like a really common question, but have yet to find a Classic ASP example.
I have data presented like the following from the database we have inherited:
120-128,10,20,30,12-19
I need to be able to convert this into a comma divided list, in consecutive order, pulling not only the numbers present, but the numbers within the ranges (specified by the -)
So in the above example, I would expect the output of:
10,12,13,14,15,16,17,18,19,20,30,120,121,122,123,124,125,126,127,128
THEN I want to be able to store that result as a single variable, so I can do more work with it later.
I have found Python Methods, C#, Javascript, PHP etc, but not one for Classic ASP. Can anyone help?
FYI, there would never be any duplicate numbers, each number will be unique.
The basic steps to do this are
At that point you have a list of all values, unsorted, and not unique.
In Classic ASP, you can use an Arraylist to help with the sorting and uniqueness. Create two arraylist objects. One will contain the non-unique list, and then the other will contain your final unique list.
<!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>