insert()
Anonymous contributor
Published Sep 20, 2023
Contribute to Docs
The .insert()
method inserts a value in a table at a specified index.
Syntax
table.insert(someTable, index, value)
The value
is inserted at index
. If index
is not provided, value
is inserted at the end of someTable
. The function returns the modified, original table.
Stacks, queues, and double queues can be implemented in this way.
Example
Adding iPhone
at the end of apple_products
table:
apple_products = {"Macbook", "Watch", "iPad"}table.insert(apple_products, "iPhone")for key, value in pairs(apple_products) doprint(key, value)end
The code above results in the following output:
MacbookWatchiPadiPhone
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.