The Lua standard libraries do not provide a function to copy a table.
However, it is relatively simple to implement such a function.

A generic table.copy function is not guaranteed to suit all use-cases, as there are many different aspects which must be selected for the specific situation.
For example: should metatables be shared or copied? Should we check userdata for a __copy metamethod?
These questions (as well as many others) must be asked and answered by the developer.

Some of the Lua "extended standard libraries", such as Penlight and stdlib provide ready-made copy functions for convenience. Check if they suit your use-case before implementing your own.

The following functions provide a base to work off of:

Shallow Copy

This a simple, naive implementation.
It only copies the top level value and its direct children; there is no handling of deeper children, metatables or special types such as userdata or coroutines. It is also susceptible to influence by the __pairs metamethod.

Additionally, it is recursive which means it may overflow the stack for large tables.

Non-recursive Deep Copy

A more flexible (non-recursive) deepcopy implementation is available from [this GitHub gist].
It allows for varying rules on how to copy special types, metatables, and function upvalues (including joining).
Please see the comments for usage.
RecentChanges · preferencesedit · historyLast edited April 16, 2013 5:20 am GMT (diff)