local function extract_range(list) if #list == 0 then return "" end local sb = "" local first = list[1] local prev = first local function append(index) if first == prev then sb ..= tostring(prev) elseif first == prev - 1 then sb ..= tostring(first) .. "," .. tostring(prev) else sb ..= tostring(first) .. "-" .. tostring(prev) end if index < #list then sb ..= "," end end for i = 2, #list do if list[i] == prev + 1 then prev += 1 else append(i) first = list[i] prev = first end end append(#list) return sb end local list1 = {-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20}print(extract_range(list1)) local list2 = { 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(extract_range(list2))