How to insert something to dictionary?

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?

Like this:
{
“test1” = true
{
[“aTest2”] = “Hello”
}
}

I want to add things into aTest2’s location

4 Likes

Have you tried the:

table.insert(aTest2, "Blah Blah Blah")

function

I think you meant table.insert(tableName.aTest2, “Blah Blah Blah”)

1 Like

Yeah, exactly what I meant. Have you tried this so far?

Actually i didn’t lemme try. 30carhsssssss

1 Like

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)
4 Likes

I hate indexing dictionaries using square brackets because of studio’s autocomplete and in general, so I just do:

local users = {
   jaipack17 = {
      points = 40,
      experience = 400,
   }
}

users.jaipack17.points = 50;
users.jaipack17.experience += 100;

The answer for your question:

local dict = {
   test1 = true,
   {
      aTest2 = "Hello"
   }
}

dict[2].aTest2 = "Something other than Hello"
1 Like

This is great except it isn’t actually inserting it into that in an easy and quick way! I feel that table.insert is easier

if aTest2 wasn’t a table, it’ll error out. table.insert is only meant to insert values to a table. Here aTest2 is a string.

1 Like

Pretty sure that’s how you do it… :confused:

So basically to insert smthg into a dictionary you would just do

dictionaryname[“newkeyname”] = newvalue

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.

Absolutely, its up to preference at the end of the day.

Can you give an example for the same?

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)
2 Likes

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?

Don’t use table.insert for that.

local dict = {}

dict.newTable = { 1, 6, 4 }

--[[
dict = {
   newTable = { 1, 6, 4 }
}
]]--
11 Likes

Okay! I will try it. Thanks. 30chhrrrsss

1 Like

I might not be understanding what this post is asking about but isn’t there a article that shows how to add and remove from dictionaries? Link

Table.insert(TableName, ("blahblahblah)) is only meant for arrays and not dictionaries.

1 Like