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

How to add a value with key to a dictionary with the same key???

Asked by 4 years ago

So what I mean is,

local dictionary = {
    ["Thing1"] = {"yuh"},
    ["Thing2"] = {"Harambe"}
    ["Thing3"] = {"Trump cheeto"}
}

suppose I have that, what if i wanted to add another "Thing3" to the dictionary, resulting like this:

local dictionary = {
    ["Thing1"] = {"yuh"},
    ["Thing2"] = {"Harambe"}
    ["Thing3"] = {"Trump cheeto"}
    ["Thing3"] = {"Trump the orang"}
}

Is it possible to do this? Is there a way around this?

0
accept my answer fat it's better User#24403 69 — 4y

2 answers

Log in to vote
1
Answered by
oreoollie 649 Moderation Voter
4 years ago
Edited 4 years ago

No, you can't add two of the same key to a table. You could however make its value a table with the elements you want. Here's an example of that:

local dictionary = {
    ["Thing1"] = "yuh",
    ["Thing2"] = "Harambe"
    ["Thing3"] = {"Trump cheeto", "Trump the orang"}
}
0
You know what, you gave me an idea, if I make 2 tales inside the single table for the key, I will successfully be able to make it look like i have 2 keys!!! greatneil80 2647 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

No. It is not.

Duplicate keys aren't possible. Lua will only use one of those key-value pairs. It wouldn't make sense for duplicates to be possible as there could be ambiguities.

Even if you try something like this:

local t = {a = 1}
t.a = 2

a field is overwritten, it now contains 2.

If you want, you can just add on to the {"Trump cheeto"} table.

table.insert(dictionary.Thing3, "Trump the orang")

Answer this question