Today I was reading the Lua 5.2 Reference Manual and it stated, "The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any Lua value except nil and NaN." Interested, I went onto studio to test this idea:
local key = {true, false, nil}
local array = {}
array[key] = "Hello World!"
print(array[key])
local secondKey = true
array[secondKey] = "Goodbye World!"
print(array[secondKey])
local dictionary = {
[key] = "Test one.";
[secondKey] = "Test two.";
}
print(dictionary[key])
print(dictionary[secondKey])
print(array.key, array.secondKey, dictionary.key, dictionary.secondKey)
Only afterwords I realized that array.key was equivalent to array["key"] and of course there was no value at the index of "key". However, the other prints were what I was really interested in. The output looked like this:
Hello World! Goodbye World! Test one. Test two. nil nil nil nil
Wow, it all worked(except the nils but I know why those are). I am now curious what an associative array is. Also, what kind of uses does this have? All I can think of is accessing a value that needs multiple keys to be accessed. As for the Boolean tests those were not as important. I mean, there are only two Boolean values, so it might be a little difficult to use them effectively in a large table. After reading down a few lines I saw that, "Like indices, the values of table fields can be of any type. In particular, because functions are first-class values, table fields can contain functions. Thus tables can also carry methods." I knew that functions could be in tables, but I am curious as to what field means. I have seen it many times and should know it by now, especially because many errors deal with fields. Further down the page it said, "The expressions a[i] and a[j] denote the same table element if and only if i and j are raw equal (that is, equal without metamethods)." I am not entirely sure what raw equal means in this instance. Finally, I was reading this, "Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain the values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy." So, I went ahead and did a test:
local a = {"Unchanged","Unchanged", "Unchanged", "Unchanged", "Unchanged"}
local b = a
for i = 1, 5 do
a[i] = "Changed"
print(b[i])
end
for i = 1, 5 do
b[i] = "Changed2"
print(a[i])
end
The output was:
Changed (x5) Changed2 (x5)
I am curious if there is an easier way to assign a and b at the beginning, but that is not the main focus of this question. I am wondering if there is a way to copy the table/function/threads/(full) userdata values instead of referencing them which may not be the best if both sides(by that I mean both a and b) can change the original list, which may cause some unknown bugs to happen because it does not throw an error. All because the code is all syntactically correct, just not logically correct.

1 comment
u simply cant