What’s the difference between using square brackets over a name and not for items in a dictionary in a dictionary?
local dictionary = {
["Hi"] = "This"
}
local dictionary = {
"Hi" = "This"
}
What’s the difference between using square brackets over a name and not for items in a dictionary in a dictionary?
local dictionary = {
["Hi"] = "This"
}
local dictionary = {
"Hi" = "This"
}
Square brackets in tables are effective for indexes with whitespaces like Hi there
. You can avoid using square brackets by writing the index as a variable inside instead.
You can’t do the second method though, that’s not defining the index of the table.
You could still do
"hi there" = "hi"
or could you not?
You can’t do that, because Lua mistakes it as setting the first string into the table and the =
is unexpected.
What’s the index of the table?
An index is what key it is. I actually meant key, where it is just something you index to acquire its value from a table.
So I’d get the value of hi there like this?
local dictionary = {
["Hi there"] = "value"
}
dictionary["Hi there"]
That’s correct. dictionary["Hi there"] == "value"
is true.
Going forwards on Operatik’s response:
dictionary.Hi
is the same as dictionary["Hi"]
> Syntax sugar.