Lua 4 中浅表拷贝

我正在为一款使用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编写的。例如,Lua 4中不存在pairs函数。此外,该函数不是递归的。如何编写一个在Lua 4中适用且是递归的等效例程?谢谢!

[编辑]

发帖已更新。

点赞
用户2858170
用户2858170

Lua 4 具有用于表格的循环语句 for。

该表格 for 语句遍历给定表的所有键值对(索引,值)。其语法如下:

  stat ::= for name `,' name in exp1 do block end

请参阅Lua 4参考手册4.4.4节

https://www.lua.org/manual/4.0/manual.html#4.4

浅拷贝程序不需要是递归的。 这只会影响按引用复制其所有成员的表值。

2017-08-22 21:59:31