Insert a table element

Verified
Added by iNTERFACEWARE

Use table.insert() to insert an element into a table

Source Code
   -- insert at the end of the table = default
   t = {[1]='one',[2]='two'}
   table.insert(t,'three')
   trace(t[3]) --> "three"
   trace(t)    --> {[1]='one',[2]='two',[3]='three'}
   
   -- insert in the middle of the table
   t = {[1]='one',[2]='three'}
   table.insert(t,2,'two')
   trace(t[2]) --> "two"   
   trace(t)    --> {[1]='one',[2]='two',[3]='three'}
Description
Use table.insert() to insert an element into a table
Usage Details

Use table.insert() to insert an element into a table. If an index is not supplied then the element is inserted at the end of the table by default.

How to use the snippet:

  • Paste the code into your script