extractRange = function(ints) result = [] idx = 0 while idx < ints.len runLen = 1 while idx+runLen < ints.len and ints[idx+runLen] == ints[idx] + runLen runLen = runLen + 1 end while if runLen > 2 then result.push ints[idx] + "-" + ints[idx+runLen-1] idx = idx + runLen else result.push ints[idx] idx = idx + 1 end if end while return join(result, ",") end function test = [ 0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39] print extractRange(test)