- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you copy a Lua table by value?
Copying a table means that we want all the values or pairs that are present in one table in another table. In Lua, there’s no standard library function that we can use to create such a table but we can create our own function to do so.
Let’s create a function in Lua that will take a table as an argument and will create a new table that will be the exact copy of the table passed as an argument to the function.
Example
Consider the example shown below as reference −
a = {} a["name"] = "mukul" a["age"] = 23 a["isWorking"] = true function table.table_copy(t) local t2 = {} for k,v in pairs(t) do t2[k] = v end return t2 end copy = table.table_copy(a) for _,v in pairs(a) do print(v) end for _,v in pairs(copy) do print(v) end
In the above example, we have a table named a which we later pass as an argument to the function named table.table_copy() and that function returns us a copy of the same table that we passed and finally I printed the values inside these tables to check whether the copied table is accurate or not.
Output
mukul true 23 mukul true 23
- Related Articles
- How do you copy a list in Java?
- How do you do a deep copy of an object in .NET?
- How to remove Lua table entry by its key?
- How do you make a shallow copy of a list in Java?
- What do you mean by Value Centric Selling?
- How to push a Lua table as an argument?
- How do you give a C# Auto-Property a default value?
- How do you copy an element from one list to another in Java?
- How to convert JSON string into Lua table?
- Table Type in Lua Programming
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- What do you mean by FOREIGN KEY and how can we use it in MySQL table?
- How to get the number of entries in a Lua table?
- How do you append a carriage return to a value in MySQL?
- How do I copy a file in python?
