Lua arrays- check some rules and print in that order

Posted by KIdeas on Sun 02 Dec 2018 10:43 PM — 9 posts, 31,335 views.

#0
I have arrays with some set rules for the print results; if table has all elements to be 1, then it should check the last element of the next table- if that value is one, then it will be removed and added to that table.Same applies to the element 0. Here's what I have:


function table_merge(t1, t2)
for _, v in ipairs(t2) do
    table.insert(t1, v)
end
end


 function getMaster(tbl, rules)
 local result = false
 for _, rule in ipairs(rules) do
    for i, v in ipairs(tbl) do
        result = v
        if tostring(v) ~= tostring(rule) then
            result = false
            break
        end
    end
    if result then break end
   end

  return result
  end

  function start(data, rules)
  local master_key, master_val
  local _temp, continue = {}, true

     for i, tbl in ipairs(data) do
        local master = getMaster(tbl, rules)

         if master and master ~= master_val then
           continue = true
         end

         if continue then
           if master then
            master_key = i
            master_val = master
           elseif tbl[#tbl] == master_val then
            tbl[#tbl] = nil
            table.insert(_temp[master_key], master_val)
           elseif master_key then
            continue = false
          end
        end
      _temp[i] = tbl
    end

    local result = {}
      for i, tbl in ipairs(_temp) do
       table_merge(result, tbl)
     end

    return table.concat(result, "")
 end

-- RULES
local rules = { 0, 1}

local data = {
  { 0, 0, 0, 0, 0, 0 },
  { 1, 1, 1, 1, 1, 0 },
  { 0, 0, 0, 8, 1, 0 },
  { 1, 1, 1, 1, 8, 8 },
  { 0, 0, 0, 0, 0, 0 },
}

 start(data, rules)


OUTPUT:

000000001111100081111188000000

The expected results should be this:

000000001111110008111188000000

How do I achieve the required results? Thanks
Amended on Mon 03 Dec 2018 08:34 AM by KIdeas
USA Global Moderator #1
Template:codetag
To make your code more readable please use [code] tags as described here.
#2
@Friendish

thanks for the hint!
Amended on Sun 02 Dec 2018 11:22 PM by KIdeas
USA Global Moderator #3
Your tables only have 6 entries in them, and the second table starts with 1, so your description doesn't match your expected output starting with 8 zeroes. The description suggests that you should start with only 7 zeroes and then a 1.
#4
the table can contain more than six elements but initially it is six. so when all the elements are 1, it will check the next immediate Table to see if the last element is also one; if true it removes it and adds to the six making seven, it will then check again for the next immediate table if the last digit is 1, if true it removes that also and adds to the seven making 8. It will check again and if false return to where it left. So here the table whose element were removed become five in number.

So I was hoping that they could satisfy the rule also if all the five elemnts are 1 or 0. But it seems only the six elements satisfy the rule...

I hope it's clear

[EDIT] Removed code tags.
Amended on Mon 03 Dec 2018 07:23 AM by Nick Gammon
Australia Forum Administrator #5

Don’t put code tags around ordinary text, just code.


if table has all elements to be 1, then it should check the last element of the next table

What next table? Do you mean “a table consisting of tables”?


In what way does this relate to MUSHclient?


Same applies to the element 0.

What “same”? Do you mean you do this if the element is 0 or 1?

Amended on Mon 03 Dec 2018 07:27 AM by Nick Gammon
#6
yeah. I'm dealing with table consisting of tables. So it checks the first table for the rules and if true compare the last element of the next table if equal to the rule value, that's either 1 or 0.

same "means" the rule applies when all the elements in a table are all either 1 or 0.

please check my code again you will understand what I mean

thanks
Amended on Mon 03 Dec 2018 08:22 AM by KIdeas
Australia Forum Administrator #7

It looks like you cross-posted to Stack Overflow which got you an answer.

#8
Yes Sir. I've got the answer now. Thanks for the response