How to change a row of numbers

I'd like to change a row of numbers like this: 2,3,4,5,6,8,10,15,16,17 to a range of numbers like this: 2-6,8,10,15-17. Could one of the more experienced scripting guys tell me how to do this in VBScript?

Here's one way:

[code]input = "2,3,4,5,6,8,10,15,16,17"
ouput = ""
first = -1

DOpus.Output "input: " & input

for i = 1 to Len(input)
for j = i to Len(input)
if mid(input,j,1) = "," then exit for
next

value = CInt(Mid(input,i,j-i))
break = False

if first = -1 then
	first = value
	last = value
elseif value = last + 1 then
	last = value
else
	break = True
end if

if break or i >= Len(input) - 1 Then
	if Len(output) > 0 then output = output & ","
	if first = last then
		output = output & first
	else
		output = output & first & "-" & last
	end if
	if last <> value and i >= Len(input) -1 Then
		output = output & "," & value
	end if
	first = value
	last = value
end if		

i = j

next

DOpus.Output "output: " & output
[/code]

Really verbose answer this time. :smiley:
Your code works like a charm. Thanks.

Is it alarming to write code for fun whilst drinking the morning coffe? o)
So, just in case you need a jscript version.. o)

[code]var inp = "0,1,2,3,4,5,6,8,10,15,16,17", out = "";

var inArr=inp.split(","), start=null, cur=null, next=null;
for(var i=0;i<inArr.length;i++){
cur=inArr[i]*1, next=null;
if ((i+1)<inArr.length) next=inArr[i+1]*1;
if (start===null) start=cur;
if (start!==null && (cur+1)===next) continue;
if (start!==null && start!==cur) out+=start+"-"+cur; else out+=cur;
if (next) out+=",";
start=null;
}
DOpus.Output("out: " + out);
[/code]

[quote]Is it alarming to write code for fun whilst drinking the morning coffe? o)[/quote]I think it is! But as long as you don't forget to drink the coffee it's not lethal. :smiley:

Thanks for your code. It looks really compact but my script is VBScript so I have to stay with Jon's version.

I guess because you're asking for verbose responds recently, Jon did verbose! o)
Have a nice week everyone! o)

I now realized that Jon's code doesn't work for variations of the row of numbers like 1,4,5,6 or 1,2,3,4 or 3,4,5,8 or 1,3,4,5,7. I'd need the code working for all combinations of a single number and a range of numbers.
Please help, I'd need hours of try & error to get it working. :slight_smile:

And mine does? If so it could be a nice exercise for you to translate js -> vbs, no? o)

Your jscript version seems to do the trick. I tried to convert it to VBScript but the output is nonsense. I need some help to get it working.
Here's my current nonsense version:

[code] inp = "0,1,2,4,5,6,8,10,15,16,17,20"
inArr = Split(inp,",")
start = ""
cur = ""
nNext = ""
out = ""

For i=0 To UBound(inArr) :Do
cur = i
nNext = ""
If i+1 < UBound(inArr) Then nNext = i+1
If start = "" Then start = cur
If Not start = "" And cur+1 = nNext Then Exit Do
If Not start = "" And Not start = cur Then
out =out & start & "-" & cur
Else
out = out & cur
End If
If nNext = "" Then out = out & ","
start =""
Loop while false
Next
DOpus.Output("out: " + out)
[/code]

Try this.. o)

[code]option explicit
dim inp : inp = "0,1,2,4,5,6,8,10,15,16,17,20,21,22"
dim nix : nix = -1
dim inarr : inarr = split(inp,",")
dim i, start, cur, nxt, outtmp, out : out = ""
start = cur = nxt = nix

for i=0 to ubound(inarr)
cur = inarr(i)*1 : nxt = nix : outtmp = ""
if ((i)<ubound(inarr)) then nxt = inarr(i+1)*1
if start = nix then start = cur
if start = nix or (cur+1) <> nxt then
if start <> nix and start <> cur then outtmp = start & "-"
out = out & outtmp & cur
if nxt <> nix then out = out & ","
start = nix
end if
next

wsh.echo("out: " & out)[/code]

Nice trick with that "do.. loop while false" construct within the for btw! o)
I did not try if it actually works, since I switched the if statement to prevent the jscript-continue statement.
But nonetheless - never seen this before. o)

Many thanks tbone, your code works like a charme. :thumbsup: [quote]Nice trick with that "do.. loop while false" construct within the for btw! o)[/quote]Google told me the trick. Can't say if it really works.

Seems to work to me :slight_smile:

input: 1,4,5,6 output: 1,4-6 input: 1,2,3,4 output: 1-4 input: 3,4,5,8 output: 3-5,8 input: 1,3,4,5,7 output: 1,3-5,7

Yes, your versions seems fine to me too. o) There's just a minor typo at the beginning, ouput instead of output when initialising. In vbs, putting "option explicit" at the top is never a bad idea to get stricter error handling for not initialized/unknown variables and things. This optional declaration check might be a minor bit vbscript has in advantage in comparison to jscript. o)

[quote]Seems to work to me[/quote]Yes, it works when I test it from a button. I should have done this earlier. I integrated the code immediately into my script and tested this. I must have confused something. When I realized that something didn't work I tested a copy from my script from a button but never the original.
Thanks to both, Jon and tbone for your great help and patience.