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

How would I access a table inside of a table?

Started by Lego Stax, 18 September 2014 - 01:30 AM
Lego Stax #1
Posted 18 September 2014 - 03:30 AM
So, as the title asks, how would I access a table inside of a table? I've already looked this up and tried what I found in CC and it failed to work.
This is what I found:


local table = {
  [1] = {
	data = "stuff",
  }
}
-- The article I read stated this:
print(table[1][data])

However, that doesn't seem to work. I know for a fact that I can do this:


local ref = table[1]
print(ref.data)

But, I want to also be able to change the data variable inside of the table. So, is there something I'm doing wrong? Or, did I get misinformation? I apologize for my ignorance in advance. :P/>
Bomb Bloke #2
Posted 18 September 2014 - 03:32 AM
This:

print(table[1]["data"])

… gets the same result as this:

print(table[1].data)

… as does this:

data = "data"
print(table[1][data])
Dog #3
Posted 18 September 2014 - 03:33 AM
try

print(table[1].data)

note the dot

EDIT: :ph34r:/> 'd


EDIT2: lol - Ask a Ninja…
Edited on 18 September 2014 - 01:37 AM
KingofGamesYami #4
Posted 18 September 2014 - 03:34 AM
The only problem you have is the fact that data is an undefined variable, so you are accessing table[ 1 ][ nil ]… which I assume do don't want to do.


local tbl = { person = { name = "Lego" } }
print( tbl.person.name )
print( tbl[ "person" ][ "name" ] )
print( tbl[ "person"].name )

Ninja'd
Edited on 18 September 2014 - 01:34 AM
Lego Stax #5
Posted 18 September 2014 - 05:33 AM
Thank you all so much! Man I feel stupid for dancing around the answer. :mellow:/> Well, I guess that's what I get for being ignorant. :lol:/> Thank you all for your help! To be honest, I was not expecting such immediate and helpful replies. Thanks again!
Dragon53535 #6
Posted 18 September 2014 - 05:55 AM
As an extra thing, for static table keys, such as the table inner inside the table outer you could do

local outer = {inner = {"hello"}}
print(outer.inner[1]) --# Output hello

Edit: Didn't realize yami showed this already, well shoot
Edited on 18 September 2014 - 03:56 AM