How do I append to a table in Lua

drewish picture drewish · Dec 12, 2014 · Viewed 91.2k times · Source

I'm trying to figure out the equivalent of:

foo = []
foo << "bar"
foo << "baz"

I don't want to have to come up with an incrementing index. Is there an easy way to do this?

Answer

rsethc picture rsethc · Dec 12, 2014

You are looking for the insert function, found in the table section of the main library.

foo = {}
table.insert(foo, "bar")
table.insert(foo, "baz")