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 8 years ago

So, I have a table that looks like this:

1local Mytable ={
2["Money"] = 500,
3["Inventory"] ={
4["Bread"] = 1,
5["Apples"] = 40,
6  },
7}

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

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

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

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

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 — 8y

1 answer

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 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:

1local Mytable ={
2["Money"] = 500,
3["Inventory"] ={
4["Bread"] = 1,
5["Apples"] = 40,
6  },
7}
8 
9Mytable.Color = "Green"

2nd way:

1local Mytable ={
2["Money"] = 500,
3["Inventory"] ={
4["Bread"] = 1,
5["Apples"] = 40,
6  },
7}
8 
9Mytable['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 — 8y
0
Glad I could help. Shawnyg 4330 — 8y
Ad

Answer this question