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

How to insert a Dictionary into a table?

Asked by 8 years ago

This won't work:

table1 = {}
table.insert(table1, ["TEST"] = "TEST1")
print(table1["TEST"])

I'm trying to insert a Dictionary into a table, I need this for my script. I also need like a lot of dictionaries, because every game it saves 1 dictionary into the table through datastores.


Thanks for helping!

0
I would think you would just do something like table1.TEST = 'TEST1' M39a9am3R 3210 — 8y
0
hmm EzraNehemiah_TF2 3552 — 8y

3 answers

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

To add a dictionary to a table, it's simple to do. You would just use the table name and then the name of the index. In this example say you wanted the index to be 'Lol' then you would put;

Tab = {}
Tab.Lol  = 'Hello' -- Lol is the index.
print(Tab.Lol) --That will Print 'Hello'

If you wanted the index to have spaces, then you would put the text inside a block like this ['Test Lol']. In the end for spaces for indexes (not sure if that's the right term), your code should look something like this;

Tab = {}
Tab['Test Lol'] = 'Hello'
print(Tab['Test Lol']) --This should also print hello.
Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

You cannot use the insert function for this purpose mainly because it works like the following example.

local list = {}
table.insert(list, "One")

list[#list + 1] = "One"

The function is only meant to be used when indexing a table with numerical indices. In order to accomplish what you wish to do, you would have to use a format similar to the following.

local list = {}
list["Key"] = "value"

Alternatively, you can actually create a table with numerical indices, and setting said indices to a table containing both a key and a value.

local list = {}
local dictionary = {["TEST"] = "TEST1"}

for key, value in pairs(dictionary) do
  list[#list+ 1] = {key = key, value = value}
  -- or
  -- table.insert(list, {key = key, value = value})
end

Log in to vote
0
Answered by 8 years ago

Numerical Indexing

What is important to realize: When you are dealing with a normal array, the elements will normally be ordered numerically. Regular arrays and tables only deal with values. Dictionaries deal with keys and values.

Let's take a look at an example of a regular array and its indices for each value:

local ar = {"I", "Am", "Steve", "@seraplz", 1,4, true}

for index,value in pairs(ar) do
print("Index Number " .. index .. " in array contains value of \"" .. tostring(value) .. "\"")
end

Your output would look like the following:

Index Number 1 in array contains value of "I"

Index Number 2 in array contains value of "Am"

Index Number 3 in array contains value of "Steve"

Index Number 4 in array contains value of "@seraplz"

Index Number 5 in array contains value of "1"

Index Number 6 in array contains value of "4"

Index Number 7 in array contains value of "true"

FOO=BAR

A common programming paradigm is the concept of FOO=BAR, which states that a key [FOO] must equal a value [BAR].

Now, when you are making an array/table out of non-numerical indices such as lettering and words, you can only naturally expect to get some different type of output. This is because you are basing your index off of keys and not automatic numerical indexing.

For example, let's take a look at a standard dictionary:

local dic = {
    ["Name"] = "Digital",
    ["ScriptingSkill"] = "Intense",
    ["beautiful"] = true,
    ["accountId"] = 3508838 
}

for key,value in pairs(dic) do
    print("Key : " .. key .. " = Value: " .. tostring(value))
end

Now, take a look at something very interesting in my output:

Key : ScriptingSkill = Value: Intense

Key : beautiful = Value: true

Key : Name = Value: Digital

Key : accountId = Value: 3508838

Although Name is the first thing I put in the dictionary, it still got printed third. This is because these indices aren't ordered numerically. Thus the compiler may end up printing out the values and keys in an order that you may not expect

To Answer Your Question

Now if you are familiar with arrays, you know that writing:

array[4] = "someValue";

will compile just properly.

We can do the same thing with dictionaries!

So to put test in its own key/value pair inside a dictionary, you can do:

local dic = {}
dic["TEST"] = "TEST1";
print(dic.TEST)
print(dic["TEST"])

Answer this question