Help with using table methods please.

Hello facepunch,
I’m just curious to ask if anyone is willing to help me, I’m trying to figure out what are the differences of these tables below and when/why would you use these methods.
[LUA]
local = myMethod1 ={
[“string”] = true – what’s the actual use of doing the square brackets in this?
}

local myMethod2 = { [var1= “hey”, var2=20]}
[/LUA]

This would give you the best understanding of tables, this is what i first looked at : https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2584.html

neither of the examples you provided is valid syntax.

for the first one however, assuming you mistyped the first equal sign; what you’re doing is initializing the table with a string-key.

calling this


local myMethod1 ={
    ["string"] = true
}

is identical to calling this


local myMethod = {}
myMethod.string = true


or this


local myMethod = {}
myMethod["string"] = true


Thank you, I’m getting a better understanding now.

[editline]2nd October 2016[/editline]

Thanks, I’ll check it out:)

I really like the official PIL guide.

https://www.lua.org/pil/2.5.html - Tables - an introduction to tables

https://www.lua.org/pil/3.6.html - Table Constructors - this is what you’re asking about

Thanks, a big help:)