Hello, im making a DataStore plugin and i wanna add dictionary feature(table inside table). There can maybe a array then a table in a table so, i want to create a table inside table and insert things in it. Can i make it with table.insert? If i can how?
Personally, how I like to mutate and add values into dictionaries:
dict = {
stuff = {
['aTest2'] = "Hello"
}
}
-- Mutating the current aTest2 "Hello" value would work like this:
dict['stuff']['aTest2'] = "Replaced"
-- Mutating aTest2 into a dictionary would work like this:
dict['stuff']['aTest2'] = {["newDictKey"] = "dictValue", ["newDictKey2"] = "dictValue2"}
-- Adding a new key value into stuff would work like this:
dict['stuff']['aTest3'] = "Value"
-- Mutating the aTest2 values dictionary key values would look like this:
dict['stuff']['aTest2']['newDictKey2'] = "dictValueReplaced"
-- You probably get the point by now.
print(dict)
Up to preference yeah, this is something I didn’t mention. The only issue I personally have is that if you’d want to insert multilevel or any dictionary keys in for example say a loop rather than manually, you’d have to use the bracket method. Personally I reserve the . to separate other types for functions calls, make it easier to read for me instead of doing ex. dict['functionname']() But yeah I can see why this is your preferred way.
dict = {}
for genCount = 1, 10 do
dict['dictKey' .. tostring(genCount)] = "Placeholder"
-- You wouldn't be able to do with with dict.generatedkey
end
print(dict)
-- Another example is if you'd want to populate a dictionary:
dict = {}
words = {"hello", "these", "are", "words"}
for _, v in ipairs(words) do
-- dict.v = "placeholder" wouldn't work. You'd have to do:
dict[v] = "placeholder"
end
print(dict)
Okay, im inserting table to table with table.insert(tableName, {})
my question is how can i name the table that i inserted? It automaticly makes it 1. How can i change it while adding or after?