kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 13:10:27 GMT
same results bro
|
|
|
Post by Mercobots on Nov 27, 2018 15:20:11 GMT
Ok try this one , this code will validate your table as well and raise a error is any value is incorrect, also convert all your values to string ! You can add more rules than 0 and 1 , the script will handle it at the very end you have 4 test testing some common issues
-- ++++++++++++++++++++++++++++++++++++++++++++ function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
-- ++++++++++++++++++++++++++++++++++++++++++++ function isMaster(tbl, val) for _, v in ipairs(tbl) do if tostring(v) ~= tostring(val) then return false end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function validateTable(data) for row, tbl in ipairs(data) do if typeOf(tbl) ~= 'table' then print("Row:" .. row .. " Should be a table instead of " .. typeOf(tbl)) scriptExit() end for col, val in ipairs(tbl) do
if typeOf(val) == 'number' then val = tostring(val) elseif typeOf(val) ~= 'string' then print("Value form [row:" .. row .. " - col:" .. col .. "] should be string or int instead of " .. typeOf(val)) scriptExit() elseif string.len(val) <= 0 then print("Value form [row:" .. row .. " - col:" .. col .. "] is empty") scriptExit() end end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function getMasterKey(data, rules) local result = {} for i, rule in ipairs(rules) do for key, tbl in ipairs(data) do if isMaster(tbl, rule) then table.insert(result, key) break end end end return #result > 0 and result or false end
-- ++++++++++++++++++++++++++++++++++++++++++++ function start(data, rules) validateTable(data) local keys = getMasterKey(data, rules) if keys then for i_key, key in pairs(keys) do for i, tbl in pairs(data) do if i > key then local rule = rules[i_key] if tbl[#tbl] == rule then table.insert(data[key], rule) table.remove(tbl, #tbl) table.insert(tbl, 'null') end end end end end
local result = {} for _, tbl in pairs(data) do for _, v in pairs(tbl) do if v == 'null' then table.remove(tbl, #tbl) end end table_merge(result, tbl) end return result end
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 1 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0, 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 1 using 0 and 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 2 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 2 using 0') print(table.concat(result, "")) print("------------------------") -- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 3 using 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 8 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 4 using 8') print(table.concat(result, "")) print("------------------------")
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 19:50:55 GMT
Ok try this one , this code will validate your table as well and raise a error is any value is incorrect, also convert all your values to string ! You can add more rules than 0 and 1 , the script will handle it at the very end you have 4 test testing some common issues
-- ++++++++++++++++++++++++++++++++++++++++++++ function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
-- ++++++++++++++++++++++++++++++++++++++++++++ function isMaster(tbl, val) for _, v in ipairs(tbl) do if tostring(v) ~= tostring(val) then return false end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function validateTable(data) for row, tbl in ipairs(data) do if typeOf(tbl) ~= 'table' then print("Row:" .. row .. " Should be a table instead of " .. typeOf(tbl)) scriptExit() end for col, val in ipairs(tbl) do
if typeOf(val) == 'number' then val = tostring(val) elseif typeOf(val) ~= 'string' then print("Value form [row:" .. row .. " - col:" .. col .. "] should be string or int instead of " .. typeOf(val)) scriptExit() elseif string.len(val) <= 0 then print("Value form [row:" .. row .. " - col:" .. col .. "] is empty") scriptExit() end end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function getMasterKey(data, rules) local result = {} for i, rule in ipairs(rules) do for key, tbl in ipairs(data) do if isMaster(tbl, rule) then table.insert(result, key) break end end end return #result > 0 and result or false end
-- ++++++++++++++++++++++++++++++++++++++++++++ function start(data, rules) validateTable(data) local keys = getMasterKey(data, rules) if keys then for i_key, key in pairs(keys) do for i, tbl in pairs(data) do if i > key then local rule = rules[i_key] if tbl[#tbl] == rule then table.insert(data[key], rule) table.remove(tbl, #tbl) table.insert(tbl, 'null') end end end end end
local result = {} for _, tbl in pairs(data) do for _, v in pairs(tbl) do if v == 'null' then table.remove(tbl, #tbl) end end table_merge(result, tbl) end return result end
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 1 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0, 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 1 using 0 and 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 2 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 2 using 0') print(table.concat(result, "")) print("------------------------") -- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 3 using 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 8 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 4 using 8') print(table.concat(result, "")) print("------------------------") thanks for the response. trying it out now
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 19:57:20 GMT
to be clearer, the table values are not constant. they are retrieved from a series of loops in my code; so the retrieved values (strings) are stored in global variables and put inside a table
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 20:37:24 GMT
Ok try this one , this code will validate your table as well and raise a error is any value is incorrect, also convert all your values to string ! You can add more rules than 0 and 1 , the script will handle it at the very end you have 4 test testing some common issues
-- ++++++++++++++++++++++++++++++++++++++++++++ function table_merge(t1, t2) for _, v in ipairs(t2) do table.insert(t1, v) end end
-- ++++++++++++++++++++++++++++++++++++++++++++ function isMaster(tbl, val) for _, v in ipairs(tbl) do if tostring(v) ~= tostring(val) then return false end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function validateTable(data) for row, tbl in ipairs(data) do if typeOf(tbl) ~= 'table' then print("Row:" .. row .. " Should be a table instead of " .. typeOf(tbl)) scriptExit() end for col, val in ipairs(tbl) do
if typeOf(val) == 'number' then val = tostring(val) elseif typeOf(val) ~= 'string' then print("Value form [row:" .. row .. " - col:" .. col .. "] should be string or int instead of " .. typeOf(val)) scriptExit() elseif string.len(val) <= 0 then print("Value form [row:" .. row .. " - col:" .. col .. "] is empty") scriptExit() end end end return true end
-- ++++++++++++++++++++++++++++++++++++++++++++ function getMasterKey(data, rules) local result = {} for i, rule in ipairs(rules) do for key, tbl in ipairs(data) do if isMaster(tbl, rule) then table.insert(result, key) break end end end return #result > 0 and result or false end
-- ++++++++++++++++++++++++++++++++++++++++++++ function start(data, rules) validateTable(data) local keys = getMasterKey(data, rules) if keys then for i_key, key in pairs(keys) do for i, tbl in pairs(data) do if i > key then local rule = rules[i_key] if tbl[#tbl] == rule then table.insert(data[key], rule) table.remove(tbl, #tbl) table.insert(tbl, 'null') end end end end end
local result = {} for _, tbl in pairs(data) do for _, v in pairs(tbl) do if v == 'null' then table.remove(tbl, #tbl) end end table_merge(result, tbl) end return result end
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 1 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0, 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 1 using 0 and 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 2 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 0 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 2 using 0') print(table.concat(result, "")) print("------------------------") -- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 1 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 3 using 1') print(table.concat(result, "")) print("------------------------")
-- ++++++++++++++++++++++++++++++++++++++++++++ -- TEST 3 -- ++++++++++++++++++++++++++++++++++++++++++++ local _rules = { 8 } local _data = { { 0, 0, 0, 8, 8, 0 }, { 1, 1, 1, 1, 1, 1 }, { 0, 0, 8, 8, 8, 0 }, { 0, 0, 0, 0, 0, 0 }, { 0, 8, 8, 8, 8, 1 }, { 0, 8, 8, 8, 7, 0 }, }
local result = start(_data, _rules) print('Test 4 using 8') print(table.concat(result, "")) print("------------------------") nice job boss. tested it out but there's only one issue, the rule is supposed to check with the table immediately after the 1,1,1,1,1,1 or 0,0,0,0,0,0 and if false continue the normal work. it shouldn't skip the next table if false to check with the next two or three tables. I hope this can be fixed then everything will be perfect.. really appreciate your support and hard work thanks
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 20:40:33 GMT
so for all the test tables, the results should have been in the same order since none met the conditions. the hint is with the next immediate table after the 1,1,1,1,1,1 or 0,0,0,0,0,0
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 21:17:31 GMT
so after the immediate table is checked and processed, it should then check for the next immediate table also if the last row value is either 1 (for rule 1) or 0 (for rule 0) and then process it. so if it checks for the next immediate and it's false then it breaks and continue from it the condition was met..
i don't know if I'm clear. cos sometimes the data looks like this:
0,0,0,0,0,0 8,8,8,8,8,0 1,1,1,8,8,0 0,0,0,8,8,0 1,1,1,1,1,0
so in this case the expected results should be: 000000000088888111880008811111
thanks
|
|
|
Post by Mercobots on Nov 27, 2018 23:18:06 GMT
hum i dont know if i miss somthing because i tested using your example above and the result is the same, so what is wrong
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 23:26:37 GMT
if the next immediate table has no 0 or 1 at the end it shouldn't continue the search. even if the next two tables have either the 1 or 0 at the end. it should jump over the immediate next table if false..
is it clear
|
|
|
Post by Mercobots on Nov 27, 2018 23:32:14 GMT
if the next immediate table has no 0 or 1 at the end it shouldn't continue the search. even if the next two tables have either the 1 or 0 at the end. it should jump over the immediate next table if false.. is it clear only on next 1 or can be on 2 or 3 and so on
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 23:32:44 GMT
hum i dont know if i miss somthing because i tested using your example above and the result is the same, so what is wrong for instance 1,1,8,8,8,8 ----a 1,1,1,1,1,1 ----b 0,0,0,0,8,8 ----c 8,8,8,8,8,1 ----d 0,0,0,0,8,8 ----e so here table b meets the rule but the next immediate table c do not have 1 at as the last value so we stop and continue with the normal loop. so it shoudn't jump over and go to table d although d is having 1 at the end so final output should be 1188881111110000880000001000088 so none of the tables was modified i hope it's clear ...thanks
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 23:34:08 GMT
if the next immediate table has no 0 or 1 at the end it shouldn't continue the search. even if the next two tables have either the 1 or 0 at the end. it shouldn't jump over the immediate next table if false.. is it clear only on next 1 or can be on 2 or 3 and so on typo *it shouldn't jump over the immediate next table if false*
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 27, 2018 23:45:37 GMT
so if the immediate next is true, it will process it and check for the next table until false. so for instance 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 1,1,1,1,8,0 ----c 8,8,8,8,8,0 ----d 0,0,0,8,8,8 ----e here (table a) meets our rule for all 0's, so it check the next immediate table(table b) if last value is 0; b is true so it is processed after that it checks for the next immediate table (table c) if true; c is also true so it is also then processed after that it checks for the next immediate table (table d) if true; table d is also true so it is processed also and after that check the next immediate table (table e); table e is false hence the search stops there and the loop continue from where it left(table b) final output will look like this: 000000000888881111888888000888 thanks
|
|
|
Post by Mercobots on Nov 28, 2018 1:22:41 GMT
so if the immediate next is true, it will process it and check for the next table until false. so for instance 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 1,1,1,1,8,0 ----c 8,8,8,8,8,0 ----d 0,0,0,8,8,8 ----e here (table a) meets our rule for all 0's, so it check the next immediate table(table b) if last value is 0; b is true so it is processed after that it checks for the next immediate table (table c) if true; c is also true so it is also then processed after that it checks for the next immediate table (table d) if true; table d is also true so it is processed also and after that check the next immediate table (table e); table e is false hence the search stops there and the loop continue from where it left(table b) final output will look like this: 000000000888881111888888000888 thanks what is the result if i have this 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 0,0,0,8,8,8 ----e 1,1,1,1,8,0 ----c 8,8,8,8,8,0 ----d and if i have this 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 0,0,0,8,8,8 ----e 1,1,1,1,1,1 ----c 8,8,8,8,8,1 ----d
|
|
kideas
Junior Member
Posts: 69
|
Post by kideas on Nov 28, 2018 1:55:04 GMT
so if the immediate next is true, it will process it and check for the next table until false. so for instance 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 1,1,1,1,8,0 ----c 8,8,8,8,8,0 ----d 0,0,0,8,8,8 ----e here (table a) meets our rule for all 0's, so it check the next immediate table(table b) if last value is 0; b is true so it is processed after that it checks for the next immediate table (table c) if true; c is also true so it is also then processed after that it checks for the next immediate table (table d) if true; table d is also true so it is processed also and after that check the next immediate table (table e); table e is false hence the search stops there and the loop continue from where it left(table b) final output will look like this: 000000000888881111888888000888 thanks what is the result if i have this 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 0,0,0,8,8,8 ----e 1,1,1,1,8,0 ----c 8,8,8,8,8,0 ----d and if i have this 0,0,0,0,0,0 ----a 8,8,8,8,8,0 ----b 0,0,0,8,8,8 ----e 1,1,1,1,1,1 ----c 8,8,8,8,8,1 ----d 000000088888000888111180888880 000000088888000888111111188888 thanks
|
|