kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 14:56:33 GMT
Help with Lua Tables
details:i have following tables
table A = {0,0,0,8,8,8}
table B = {1,1,1,1,1,1}
table C = {0,0,8,8,8,1}
table D = {0,0,0,8,8,1}
table E = {0,8,8,8,8,8}
so in concatenating all the values in the tables this is the expected results:
results = {0,0,0,8,8,8,1,1,1,1,1,1,1,1,0,0,8,8,8,0,0,0,8,8,0,8,8,8,8,8}
thanks
|
|
|
Post by Mercobots on Nov 26, 2018 15:09:36 GMT
try this
function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
tableA = {0,0,0,8,8,8} tableB = {1,1,1,1,1,1} tableC = {0,0,8,8,8,1} tableD = {0,0,0,8,8,1} tableE = {0,8,8,8,8,8} result = {}
table_merge(result,tableA) table_merge(result,tableB) table_merge(result,tableC) table_merge(result,tableD) table_merge(result,tableE)
print(result)
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 15:43:42 GMT
please check the results; there's a condition if one table has all values to be 1
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 15:44:01 GMT
try this function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
tableA = {0,0,0,8,8,8} tableB = {1,1,1,1,1,1} tableC = {0,0,8,8,8,1} tableD = {0,0,0,8,8,1} tableE = {0,8,8,8,8,8} result = {}
table_merge(result,tableA) table_merge(result,tableB) table_merge(result,tableC) table_merge(result,tableD) table_merge(result,tableE)
print(result) the condition is that if a table has all values to be 1,(so here Table B), then it should check the 6th value of the next table(Table C) if it is also 1. If true then that 1 will be be recorded right after the 6 (1,1,1,1,1,1) values of Table B creating 7 1's i.e (1,1,1,1,1,1,1,); it will then check gain if the 6th value of the next Table(Table D) is 1; if true then that 1 is recorded after the previous 7 1's making 8 1's i.e (1,1,1,1,1,1,1,1); it will then check gain if the 6th value of the next table(Table D) is 1; if false then it jumps back to the first value of table right after the Table with 6 1's (i.e Table C) and continue the recording; the 6th value is exempted if recorded already. so for Table C and D the values 1-5 will be recorded: so we have the final results like this: results = {0,0,0,8,8,8,1,1,1,1,1,1,1,1,0,0,8,8,8,0,0,0,8,8,0,8,8,8,8,8}
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 16:24:27 GMT
is it clear now??
|
|
|
Post by Mercobots on Nov 26, 2018 16:35:21 GMT
ok first i did is very fast so is not good looking , and i needed some modification on your tables
function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
function all1(tbl) for i, v in ipairs(tbl) do if v ~= 1 then return false end end return true end
local myList = { { 0, 0, 0, 8, 8, 8 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 1 }, { 0, 0, 0, 8, 8, 1 }, { 0, 8, 8, 8, 8, 8 }, }
local _result, _temp = {}, {} local _key_master
for k, tbl in pairs(myList) do if not _key_master then if all1(tbl) then _key_master = k else table_merge(_result, tbl) end else if tbl[#tbl] == 1 then table.insert(myList[_key_master], 1) table.remove(tbl, #tbl) end table_merge(_temp, tbl) end end table_merge(_result, myList[_key_master]) table_merge(_result, _temp)
print(_result) scriptExit()
tested here and the result is correct
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 17:11:04 GMT
ok first i did is very fast so is not good looking , and i needed some modification on your tables
function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
function all1(tbl) for i, v in ipairs(tbl) do if v ~= 1 then return false end end return true end
local myList = { { 0, 0, 0, 8, 8, 8 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 1 }, { 0, 0, 0, 8, 8, 1 }, { 0, 8, 8, 8, 8, 8 }, }
local _result, _temp = {}, {} local _key_master
for k, tbl in pairs(myList) do if not _key_master then if all1(tbl) then _key_master = k else table_merge(_result, tbl) end else if tbl[#tbl] == 1 then table.insert(myList[_key_master], 1) table.remove(tbl, #tbl) end table_merge(_temp, tbl) end end table_merge(_result, myList[_key_master]) table_merge(_result, _temp)
print(_result) scriptExit()
tested here and the result is correct you are best! checking it out now. really appreciate the support!
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 26, 2018 21:22:22 GMT
ok first i did is very fast so is not good looking , and i needed some modification on your tables
function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
function all1(tbl) for i, v in ipairs(tbl) do if v ~= 1 then return false end end return true end
local myList = { { 0, 0, 0, 8, 8, 8 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 1 }, { 0, 0, 0, 8, 8, 1 }, { 0, 8, 8, 8, 8, 8 }, }
local _result, _temp = {}, {} local _key_master
for k, tbl in pairs(myList) do if not _key_master then if all1(tbl) then _key_master = k else table_merge(_result, tbl) end else if tbl[#tbl] == 1 then table.insert(myList[_key_master], 1) table.remove(tbl, #tbl) end table_merge(_temp, tbl) end end table_merge(_result, myList[_key_master]) table_merge(_result, _temp)
print(_result) scriptExit()
tested here and the result is correct please I'm getting this error: bad argument#1 to 'ipairs' (table expected, got function) i traced and it seems the error is here function table_merge(t1, t2) for _, v in ipairs(t2) do -------error here table.insert(t1, v) end end please help
|
|
|
Post by Mercobots on Nov 26, 2018 22:14:38 GMT
You tested the code at it is? Or you had change somthing else because here it works perfectly
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 0:07:14 GMT
You tested the code at it is? Or you had change somthing else because here it works perfectly I didn't change anything. I only added additional tables. I will recheck my code once again. Thanks
|
|
|
Post by Mercobots on Nov 27, 2018 0:20:01 GMT
how are you printing the result ?
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 0:42:06 GMT
still getting error bad argument bad argument#1 to 'ipairs' (table expected, got function)
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 0:43:42 GMT
how are you printing the result ? I am first assigning it to a local variable before print..maybe I might be wrong there
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 0:56:03 GMT
how are you printing the result ? I have checked it out. everything is as you wrote but I'm still getting the same error: bad argument bad argument#1 to 'ipairs' (table expected, got nil) traced to this function: function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
|
|
|
Post by Mercobots on Nov 27, 2018 1:01:58 GMT
ok do the following
change
print(_result)
to
print(table.concat(_result,","))
share your whole table and if possible a SS
|
|