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

Not really related to CC but about tables in lua.

Started by chardo440, 03 April 2014 - 06:50 PM
chardo440 #1
Posted 03 April 2014 - 08:53 PM
So I'm working on learning lua. So much reading but gonna be worth it especially since I'm learning it not just for Minecraft but for possible apps and such. After I learn this I'm gonna move onto java or C. However I came across a few different types of table modifications. So if I have…


Wire = {}

Wire.Red = "redWire"

or

Wire[Red] = "redWire"


What is it that doesn't let me call either or? I'm sure the answer is obvious but if I have a table and I'd want to use the wire.Red to make a modification or to add or remove but I'd like to call them with the brackets why can't I do that? Sorry in advance trying to learn everything.

So I'm working on learning lua. So much reading but gonna be worth it especially since I'm learning it not just for Minecraft but for possible apps and such. After I learn this I'm gonna move onto java or C. However I came across a few different types of table modifications. So if I have… Wire = {} Wire.Red = "redWire" or Wire[Red] = "redWire" What is it that doesn't let me call either or? I'm sure the answer is obvious but if I have a table and I'd want to use the wire.Red to make a modification or to add or remove but I'd like to call them with the brackets why can't I do that? Sorry in advance trying to learn everything.

I think I literally just figure it out that Wire["Red"] is the same as Wire.Red I guess it's so that you can use the same variable with a bit different output or something?
CometWolf #2
Posted 03 April 2014 - 08:56 PM
tableName.string can only be used with strings. It's just syntactic sugar for tableName["string"]. Whereas tableName[variable] is only for pre-defined variables or numbers.
chardo440 #3
Posted 03 April 2014 - 09:41 PM
tableName.string can only be used with strings. It's just syntactic sugar for tableName["string"]. Whereas tableName[variable] is only for pre-defined variables or numbers.


Oh wow, Thank you! That makes a ton more sense. Now I know why it works ! Thanks again!
Bomb Bloke #4
Posted 03 April 2014 - 10:28 PM
tableName.string is also ever so slightly faster to execute, as Lua doesn't need to work out what type "string" is; it can only be a string.

Take note that Wire.Red is not the same thing as Wire[Red], but is the same thing as Wire["Red"].
Lyqyd #5
Posted 04 April 2014 - 02:29 AM
Take note that Wire.Red is not the same thing as Wire[Red], but is the same thing as Wire["Red"].

With the obvious caveat that if someone has declared Red = "Red" beforehand, they are indeed the same thing. That would be a silly thing to do, though.