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

Tables In Tables

Started by blipman17, 03 October 2013 - 03:47 PM
blipman17 #1
Posted 03 October 2013 - 05:47 PM
i'm, trying to acces a table in a table, but i can't get it right.
i have the following piece of code just to try it.

stonestairs={}
craftables={stonestairs}
textutils.tabulate{craftables}
i want to be able to store a yet unknown amounth of tables in craftables, but i do want to access the value's of certain keys in those tables. I just can't get it working, could anyone help me? that would be wonderfull.

my excuses for my English level, I'm Dutch.
Bubba #2
Posted 03 October 2013 - 06:27 PM
You're using the function textutils.tabulate without parentheses, which are required. It should be

textutils.tabulate(craftables)

As your code is given though, this will not output anything due to the fact that both tables are empty.


Also, though assigning a table to a variable and then inserting it into another table is valid, you can use several other methods that may make your life simpler.
1) table.insert(table, item)
Example:

local craftables = {}
table.insert(craftables, {"This", "Has","Stuff"})

2) table[index] = value
Example:

local craftables = {}
craftables[1] = {"Table", "Within", "A", "Table"}

3) Use the "dot" operator
Example

local craftables = {}
craftables.someTable = {"This","is","for","named","tables"}

4) Directly insert into the table
Example

local craftables = {
  [1] = {"Hello!"};
  [2] = {"Stuff", "Table"};
}
AgentE382 #3
Posted 03 October 2013 - 11:50 PM
Uh, Bubba… You can call Lua functions with a single table argument, omitting the parentheses. This:
textutils.tabulate{craftables}
is equivalent to this:
textutils.tabulate({craftables})
It constructs a new table with a reference to craftables stored as index 1.

All of his calls condense to this:
textutils.tabulate({[1] = {[1] = {}}})

I think any of your options would definitely make his code simpler.