我正在为使用 Lua 4 的旧视频游戏制作模组,我需要一种方法来创建输入表的浅表副本。我在网上找到了这个程序:
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
但是,该例程是为更高版本的 Lua 编写的。例如,该pairs
函数在 Lua 4 中不存在。此外,该函数不是递归的。我将如何编写一个在 Lua 4 中工作并且是递归的等效例程?谢谢!
[编辑]
更新帖子。