Jump to content




Referencing A Table Within A Table


  • You cannot reply to this topic
6 replies to this topic

#1 Wing

  • Members
  • 58 posts

Posted 19 October 2013 - 01: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!

#2 VADemon

  • Members
  • 11 posts

Posted 19 October 2013 - 02: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)


#3 Goof

  • Members
  • 751 posts

Posted 19 October 2013 - 02: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

:)

#4 Wing

  • Members
  • 58 posts

Posted 19 October 2013 - 03: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)

#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 19 October 2013 - 04: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


#6 Wing

  • Members
  • 58 posts

Posted 19 October 2013 - 06: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?

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 19 October 2013 - 06:36 PM

View PostWing, on 19 October 2013 - 03:35 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users