Lua 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.
Learn Lua on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
- Beginner Friendly.4 hours