#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"])
2 comments
I would think you would just do something like table1.TEST = 'TEST1'
hmm