local Table = {1,2,3,4,"FDS",5,6,7,8,9,10} local Result = nil local FirstNums = {} local LastNums = {} local OtherNum = {} for i,v in pairs(Table) do if tonumber(v) then if table.getn(LastNums) == 0 then table.insert(LastNums,v) table.insert(FirstNums,v) else if (FirstNums[table.getn(FirstNums)] + 1) == v then table.insert(LastNums,v) else table.insert(OtherNum,v) end end else print("NOT A NUMBER") end end for i = 1,table.getn(FirstNums) do if (FirstNums[i] + 1) == LastNums[i] then print(FirstNums[i].."-"..LastNums[i]) else for _,v in pairs(OtherNum) do print(v) end end end
doesn't print any errors
Output:
NOT A NUMBER
3
4
5
6
7
8
9
10
Not sure what your goal is but here's the script with my comments. As you can see, table.getn(FirstNums)
is always 1.
local Table = {1,2,3,4,"FDS",5,6,7,8,9,10} local Result = nil local FirstNums = {} local LastNums = {} local OtherNum = {} for i,v in pairs(Table) do if tonumber(v) then if table.getn(LastNums) == 0 then print("-Storing: ",v) table.insert(LastNums,v) table.insert(FirstNums,v) else print("-FirstNum is always one: ",FirstNums[table.getn(FirstNums)]) -- if 1 + 1 == v then if (FirstNums[table.getn(FirstNums)] + 1) == v then print("-Storing LastNum: ",v) table.insert(LastNums,v) -- Will always store 2 else print("-Storing OtherNum: ",v) table.insert(OtherNum,v) end end else print("NOT A NUMBER") end end -- For loop iterates once for i = 1,table.getn(FirstNums) do print("-First: ",FirstNums[i] + 1," : Last: ",LastNums[i]) -- If 1 + 1 == 1 then if (FirstNums[i] + 1) == LastNums[i] then print(FirstNums[i].."-"..LastNums[i]) else print("-OtherNum") for _,v in pairs(OtherNum) do print(v) end end end
Did you mean to do this?
for i = 1,table.getn(LastNums) do if (FirstNums[1] + 1 == LastNums[i]) then print(FirstNums[1].."-"..LastNums[i]) else print("-OtherNum") for _,v in pairs(OtherNum) do print(v) end end end