This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Wing's profile picture

Referencing A Table Within A Table

Started by Wing, 19 October 2013 - 12:57 PM
Wing #1
Posted 19 October 2013 - 02:57 PM
Hey guys, I just can't come to understand how to make a table within a table and give it values with out doing it 1 by 1.
I want:
buttons = {"off" {19,8,22,8}, "enter" {17,5,22,5}, "input" {5,5,17,5}} 
So that I can use it like so:

print(buttons["off"[1]]) --buttons["off"] is equal to buttons[1] right?
result = 19
Any help is appreciated!
VADemon #2
Posted 19 October 2013 - 03:08 PM
What first line should look like:
buttons = {["off"] = {19,8,22,8}, ["enter"] = {17,5,22,5}, input = {5,5,17,5}}
Just don't forget [] if you put the name inside ""
buttons = {["off"] = {19,8,22,8}, ["enter"] = {17,5,22,5}, input = {5,5,17,5}}

print(buttons["off"][1]) --buttons["off"] is equal to buttons[1] right?
result = 19
print("Result: " ..result)
Goof #3
Posted 19 October 2013 - 03:12 PM
EDIT: uurgh! I got ninja'd by VADemon

Well… To create a normal table you just do as you did…

first assign the "name" of the table and then make the brackets…
Then you do the exact same, if you want a table inside..

Example:

buttons = {
  ["off"] = { -- make the brackets around the name of this new table. -- then assign values to the table
	19,
	8,
	22,
	8,
  };
}; -- btw you can also do a table like this. (multiline)

Then you can print the "22" by:

print(buttons["off"][3]) -- the number here is the value in the "off" table.. (well you already know that)


And you can also add infinite ( well almost ) tables inside a table like this:

local tbl = {
  value1 = "VAL1";
  ["tbl2"] = {
    value2 = "VAL2";
    ["tbl3"] = {
      value3 = "VAL3";
      ["tbl4"] = {
        value4 = "VAL4";
        --- etc.....
      };
    };
  };
};

print(tbl["tbl2"]["tbl3"]["tbl4"].value4)

I hope I could help

:)/>
Wing #4
Posted 19 October 2013 - 04:35 PM
Ok, but I don't seem to understand how to use it…

  if (sEvent == "mouse_click") then
   for i=1,3 do
	local atyes = w.coordCheck(p2,p3,buttons[i][1],buttons[i][2],buttons[i][3],buttons[i][4])
	if (atyes == true) then
	 functions[buttons[i]]()
	end
   end
Although, at line 3 (in the example above) I receive "attempt to index? (a nil value)
Kingdaro #5
Posted 19 October 2013 - 05:19 PM
I think this is what you're looking for:

buttons = {
   off = {19,8,22,8},
   enter = {17,5,22,5},
   input = {5,5,17,5}
}

Yeah, you don't need to put quotes around your table names or anything, you can just do that. To access the numbers of the tables inside the table (the first number for example), you can either do this directly:
print(buttons.off[1]) --> 19
or through a string:
print(buttons["off"][1]) --> 19

It also works if you put that string to a variable, and use that string to access the table.

str = "off"
print(buttons[str][1]) --> 19

What doesn't work is this:

str = "off"
print(buttons.str[1]) --> error
Because lua will actually look for buttons["str"], and not buttons[str].

buttons[1] will not find buttons.off, because buttons.off is not stored at 1 in the table, but at "off". To loop through these buttons, you use pairs().

for name, button in pairs(buttons) do
   print(name, button[1])
end

When looping through, "name" is the name of the button (off, enter, etc.), and "button" is the table inside the table. "button[1]" is the first value of the table inside the table. The above code will print:

off 19
enter 17
input 5
Wing #6
Posted 19 October 2013 - 07:27 PM
Alright thank you guys! I've managed to get it to work with all your help.
Although since we're here, anyway to make it so term.write does not write with the background of the color which was last in use?
Kinda like making it transparent, you know?
Bomb Bloke #7
Posted 19 October 2013 - 07:36 PM
Ok, but I don't seem to understand how to use it…

  if (sEvent == "mouse_click") then
   for i=1,3 do
	local atyes = w.coordCheck(p2,p3,buttons[i][1],buttons[i][2],buttons[i][3],buttons[i][4])
	if (atyes == true) then
	 functions[buttons[i]]()
	end
   end
Although, at line 3 (in the example above) I receive "attempt to index? (a nil value)

If you specifically want to be able to use something like that, then you'd create your table like this:

buttons = { {19,8,22,8}, {17,5,22,5}, {5,5,17,5} }

You could also make that a "for i=1,#buttons do" to have it automatically loop once for every entry in the buttons table. Using pairs() as Kingdaro demonstrated allows you to more descriptively name your table entries, however.

Re the colours, you'll need to keep track of which background colour you want to use where each time you write more text. You can't have it automatically match the colour that's already in place.