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

Access tables from inside other tables

Started by MatazaNz, 23 June 2013 - 10:50 PM
MatazaNz #1
Posted 24 June 2013 - 12:50 AM
Hiya, I'm trying to access information from a table, which is inside another table.
Here is my table:

this = {
  coordsKnown = 0,
 
  xCoord = 0,
  zCoord = 0,
  yCoord = 0,
 
  face = 1,
  faces = {"north", "east", "south", "west"},
 
  zDiff = {-1, 0, 1, 0},
  xDiff = {0, 1, 0, -1}
}

What I'm trying to do is access info from the faces table to print it to the screen, but it always comes up nil. Is there an easy way to do this? Below are ways I've tried (and failed)

this.faces[face]

this[faces[face]]

this[faces][face]


Any help is appreciated.
VADemon #2
Posted 24 June 2013 - 01:12 AM
You want to get the value of "face" that is inside table "this":
this[face]

It's a number.
So the code is:
this[faces][this[face]]
or
this.faces[ this.face ]
Btw you can use [] or . to point at a table key ( but . requires just normal letters while ["bla534fs#sd!"] may contain any string)
MatazaNz #3
Posted 24 June 2013 - 01:16 AM
Oh, I see where I was going wrong now, when I was just using

this.faces[face]
It was trying to access the faces table inside the this table, and comparing it with face, but I hadn't let it know face was in the this table as well.

Thank you, VADemon
Lyqyd #4
Posted 24 June 2013 - 01:45 AM
A note:

this.face and this[face] are not the same thing (unless the variable face contains the string "face"), but this.face and this["face"] are the same. Please be careful which one you mean. In this case, the answer OP is looking for is:

this.faces[this.face]
MatazaNz #5
Posted 24 June 2013 - 03:03 AM
Thank you, I used this.face[this.face] and it all works perfectly. Now time to find another part where it can break