Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Lua arrays- check some rules and print in that order

Lua arrays- check some rules and print in that order

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by KIdeas   (5 posts)  Bio
Date Sun 02 Dec 2018 10:43 PM (UTC)

Amended on Mon 03 Dec 2018 08:34 AM (UTC) by KIdeas

Message
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
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Sun 02 Dec 2018 11:16 PM (UTC)
Message
Template:codetag To make your code more readable please use [code] tags as described here.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by KIdeas   (5 posts)  Bio
Date Reply #2 on Sun 02 Dec 2018 11:21 PM (UTC)

Amended on Sun 02 Dec 2018 11:22 PM (UTC) by KIdeas

Message
@Friendish

thanks for the hint!
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Mon 03 Dec 2018 12:28 AM (UTC)
Message
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.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by KIdeas   (5 posts)  Bio
Date Reply #4 on Mon 03 Dec 2018 02:07 AM (UTC)

Amended on Mon 03 Dec 2018 07:23 AM (UTC) by Nick Gammon

Message
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.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #5 on Mon 03 Dec 2018 07:25 AM (UTC)

Amended on Mon 03 Dec 2018 07:27 AM (UTC) by Nick Gammon

Message

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?


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by KIdeas   (5 posts)  Bio
Date Reply #6 on Mon 03 Dec 2018 08:05 AM (UTC)

Amended on Mon 03 Dec 2018 08:22 AM (UTC) by KIdeas

Message
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
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #7 on Mon 03 Dec 2018 09:28 PM (UTC)
Message

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


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by KIdeas   (5 posts)  Bio
Date Reply #8 on Tue 04 Dec 2018 03:27 AM (UTC)
Message
Yes Sir. I've got the answer now. Thanks for the response
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,485 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.