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

How do I insert a value with a key into a table using table.insert?

Asked by 9 years ago

Ok so I want to insert a value with a key into a table, like so:

local Test = {}
table.insert(Test,["Test1"] = {"Test Table"})

But it always gives me a syntax error. How would I do this?

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

table.insert is just a normal function -- you give it values, not syntax constructs.


However, it is only used for inserting at the whole numbered indexes. Just set:

Test["Test1"] = {"Test Table"}
-- or, equivalently
Test.Test1 = {"Test Table"}

If it was a number index, using table.insert would look like this:

table.insert(Test, 5, {"Test Table"})
0
Thanks! Really appreciate your help TurboFusion 1821 — 9y
Ad

Answer this question