I'm making a mod for an old video game that uses Lua 4, and I need a way to create a shallow copy of an inputted table. I found this routine on the web:
http://lua-users.org/wiki/CopyTable
function shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
However, the routine was written for a later version of Lua. For instance, the pairs function does not exist in Lua 4. Also, the function is not recursive. How would I write an equivalent routine that works in Lua 4 and is recursive? Thanks!
[edit]
Updated post.
Lua 4 has a for loop for tables.
The table for statement traverses all pairs (index,value) of a given table. It has the following syntax:
stat ::= for name `,' name in exp1 do block end
Refer to the Lua 4 reference manual section 4.4.4
https://www.lua.org/manual/4.0/manual.html#4.4
A shallow copy routine does not need to be recursive. This would only affect table values which are copied by reference and hence have all their members on-board.