如何在 Lua 循环中强制进行新的赋值?

我想知道如何在循环中强制进行新的赋值:

for file in lfs.dir( lfs.currentdir() .. "/content" ) do
    if lfs.attributes( lfs.currentdir() .. "/content/" .. file, "mode" ) == "file" then
        if file:sub( 0, 1 ) ~= "." then

            local article = Article:new( lfs.currentdir() .. "/content/" .. file )
            table.insert( self.articles[article.lang], article )

        end
    end
end

当我通过调试器运行此代码片段时,我可以看到article变量在内存中始终具有相同的地址,因此self.articles表的每个元素都完全相同。

如何强制分配一个新的内存空间,而不删除旧的内存空间(应该在表中引用)?

编辑

我目前使用30log: https://github.com/Yonaba/30log

Article继承自Content类:

content.lua(部分)

local content = class()
content.__name = "Content"

function content:__init( file_path )

    self.title    = _( "Untitled document" )
    -- ... other declarations like this, nothing less, nothing more

end

-- Some methods follow

article.lua(完整)

local article = Content:extends()

article.__name = "Article"

function article:__init( file_path )

    article.super:__init( file_path )

end

return article

编辑2

可以在此处“查看上下文”:https://github.com/martin-damien/frankenstein/blob/master/pieces/site.lua#L151

谢谢,

达米恩

点赞
用户204011
用户204011

你的 GitHub 仓库中的代码存在几个问题,但与此问题可能有关的一个问题是 这个。Lua 表索引从 1 开始,因此你将 nil 赋值为文章的默认语言…

如果我修复了这个问题以及其他像你无法在构造函数中 调用方法 使用 30log 的问题,你的代码“可以工作”(即在此循环后,相关表格中会有几个文章)。

2013-06-18 13:08:57