Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to insert Dictionaries into Tables?

Asked by 7 years ago

So, I have a table that looks like this:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

So i'm trying to insert another Dictionary like this:

["Pets"] ={
["Dogs"] = 1,
["Cats"] = 5,
},

But how would I insert that Dictionary into Mytable? I already tried :

table.insert(Mytable,["Pets"] ={
["Dogs"] = 1,
["Cats"] = 5,
},)

But that didn't work... Anyone know how to do this?

0
I don't believe this is possible unless you put it into the script itself. antonio6643 426 — 7y

1 answer

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
7 years ago

Great question. As you already know, table.insert() will not work with dictionaries! To add values or definitions to your table, there are 2 ways.

1st way:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

Mytable.Color = "Green"

2nd way:

local Mytable ={
["Money"] = 500,
["Inventory"] ={
["Bread"] = 1,
["Apples"] = 40,
  },
}

Mytable['Color'] = "Green"

Adding values to a dictionary is indexing it, which is practically the same way you define it! Hope I've helped.

0
Thank you! MRbraveDragon 374 — 7y
0
Glad I could help. Shawnyg 4330 — 7y
Ad

Answer this question