table = {};
table["a"] = value1;
table["b"] = value2;
etc..
but this doesn't:
table = {"a" = value1, "b" = value2, etc...};
Appreciate the help.
table = {};
table["a"] = value1;
table["b"] = value2;
etc..
but this doesn't:
table = {"a" = value1, "b" = value2, etc...};
table = {"a" = value1, "b" = value2, etc...};
tbl = {a = "value1", b = "value2"} --#Don't use the word table for your tables
--#Or you could do
tbl = {["a"] = "value1",["b"] = "value2"}
local myTable = { a = value1, b = value2 , etc...};
tbl = {["a"] = "value1",["b"] = "value2"}
A little tip when using tables that have string keys that you already know.I was just using the word table as an example, and I was trying to assign table["a"] to a value, so I purposefully made the a in quotes. Thanks for the help, and dragon's second example workedtbl = {["a"] = "value1",["b"] = "value2"}
tbl.a == "value1"
tbl["b"] == "value2"
Notice though, that you can't use a variable to search for the value, you have to hard code it, so the first is not good if you're having changing key's, however for static keys, such as in this example, you can easily reference them.
tbl = {["a"] = "value1",b = "value2"}
It's the same, they do the same thing, however one is technically hard coded, and the other you can use a string variable in it's place.
--# table creation
local tbl = {
"value", --# entries without a key will be stored under an index
[1] = "value", --# keys/indexes can be in [ ]
[-4] = "value", --# it is possible to use 0 or negative indexes with [ ]
["key"] = "value", --# this is the most verbose definition
key = "value", --# and can actually be simplified to this
key1 = "value", --# you can even do numbers, just not at the start, 1key would be a parser error
["a key"] = "value",--# any key with non-alphanumeric chars must be done like this
key = "value"; --# table entries can be separated by a ; or ,
}
--# table access
print( tbl[1] ) --# must be used for any index (positive, 0, or negative)
print( tbl["key"] ) --# can be used for keys too
print( tbl.key ) --# keys can have a shorthand, as long as they're alphanumeric
local array_table = { "one", "two", "three" }
local associative_table = { one=1, two=2, three=3 }
local mixed_table = { "one", "two", three=3 }
print ("length of array=", #array_table) -- number of items in array table
print ("length of associative_table=", #associative_table) -- zero, because this is not an array
print ("length of mixed_table=", #mixed_table) -- number of items in table that are part of the array (notice: 2, not 3)
print ("ipairs on array:")
for index, value in ipairs (array_table) do -- ipairs only works with arrays
print (index, value)
end
print ("ipairs on mixed table:")
for index, value in ipairs (mixed_table) do -- notice how it skips "three"?
print (index, value)
end
print ("ipairs on associative table:")
for index, value in ipairs (associative_table) do -- Prints nothing!
print (index, value)
end
print ("pairs on associative table (notice the order):")
for key, value in pairs (associative_table) do -- Returns nothing!
print (key, value)
end