I am working on a table where I need to make a custom index, how exactly would I be able to do this?
✓ ANSWER ACCEPTED
Is it possible to use table.insert() with a custom index?
1 answer
✓ Accepted answer
The table library currently does not operate on dictionaries. Now, more along the lines of the spirit of your enquiry, you can use this function:
local function setKeyAndValue(t, k, v)
t[k] = v
end
local tbl = {}
setKeyAndValue(tbl, "Key", 2)
print(tbl.Key)
Which just sets the key and values. Pretty self explanatory.
#Fun fact
With table.insert(list, pos, value) you can set value to be at position pos. insert shifts the indices to the right by 1 if the index is not nil.
##Examples
local tbl = {"hello", "world", "yes", "no"}
table.insert(tbl, 1, "goodbye")
print(table.concat(tbl, ", "))
--> goodbye, hello, world, yes, no
goodbye is placed at index 1, and hello, formerly index 1, is shifted down so is everything else.
####Don't forget to accept if this helps
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
What is a custom index to you
instead of having my index as a number I want to make it a string
No. The table library operates on arrays only.
You'd have to make your own function and that isn't too hard
How exactly would I do that?