【问题标题】:How do I do the equivalent to passing parameters by value in Lua?我如何做相当于在 Lua 中按值传递参数?
【发布时间】:2017-06-18 17:27:37
【问题描述】:

我是 Lua 的新手,我只是尝试做一些在其他语言中常见且简单的事情,但在 Lua 中的工作方式不同,因为函数的参数是通过引用传递的(我假设)。似乎也通过引用来向表中添加内容。伪代码:

objImage --stores details about each image like name, iso, aperture etc.
tblMetadata --table that has all the image names and associated data.
tblImages --table to hold the image objects (objImage)

for each line in tblMetadata
  objImage.name = blahblah
  objImage.iso = blahblah
  etc...
  table.insert(tblImages, objImage)
  objImage = nil 
end

在我使用过的大多数语言中,objImage = nil(或等效语言)会重置对象以允许将新图像添加到表中。但在 Lua 中,它将刚刚添加到表中的对象设置为 nil。以这样的迭代方式将一系列“对象”添加到表中的技术是什么?我尝试使用第二个 objImage (objImage2) 并将 objImage 分配给它,然后再将它 (objImage2) 添加到表中,但这只是将指针/引用分配给原始 objImage。

编辑:我的伪代码没有完全反映我想要做什么,所以我在下面添加了实际代码:

function extractExif(tblOutput)
    local tblImages = {}
    local blnFlag = false
    local intCount = 0
    local Image = {}  --pseudo object to hold metadata for each image

    for k,v in pairs(tblOutput) do  --iterate through each value in the table
        if string.find(v, "^=.+") then
            --test if new image other than the first one
            if blnFlag == true then
            --add Image to tblImages and then clear Image object
            table.insert(tblImages, Image)
            Image = nil
            blnFlag = false
            end

            i, j = string.find(v, "/")  -- **** MAC ONLY. Back slash for Windows *****
            Image.filePath = string.sub(v, i)   --returns the file path
            --Image.name = string.match(v, "([^/]+)$")  --return the file name
            blnFlag = true

        elseif string.find(v, "ISO") ~= nil then
            Image.iso = string.match(v, "%a+:(.+)") --get text (i.e value) to right of colon
        elseif string.find(v, "Film") ~= nil then
            Image.filmSim = string.match(v, "%a+:(.+)")
        elseif string.find(v, "Setting") ~= nil then
            Image.drMode = string.match(v, "%a+:(.+)")
        elseif (string.find(v, "Auto") ~= nil) or (string.find(v, "Development") ~= nil) then  -- corresponds to "Auto Dynamic Range" and "Development Dynamic Range" in fuji exif
            Image.dr = string.match(v, "%a+:(.+)")
        else

        end
    end
end

我当然可以通过使用嵌套表等而不是平面 tblOutput 元数据列表来更好地编程,我可能会在某个时候这样做。

【问题讨论】:

  • 这段代码正确吗? objTable 变量是什么?你能贴一些真实的代码吗?
  • 对不起,应该是 objImage。我已经更正了。

标签: for-loop lua lua-table


【解决方案1】:

可能您必须使用空表而不是 nil 值重置 objImage

例如:

local objImage = {}
local tblMetadata = {'foo', 'bar', 'biz'}
local tblImages = {}

for k, v in ipairs(tblMetadata) do
  objImage.name = v
  objImage.iso = v
  table.insert(tblImages, objImage)
  objImage = {}
end

【讨论】:

  • 啊,伙计,我确定我已经尝试过了。是的,这解决了它。谢谢。
  • 将 objImage 的声明移到循环内会使局部性更清晰。这样你就不需要最后的任务了。您甚至可以使用表构造函数使用表的两个字段填充表。然后你可以内联表构造函数,这样循环体只有一个语句。 table.insert(tblImages, { name = v, iso = v })
  • 查看我对 Egor 的回答@TomBlodget 的回复
【解决方案2】:
for each line in tblMetadata
  -- Create new LOCAL empty instance
  local objImage = {}
  -- Fill the data
  objImage.name = blahblah
  objImage.iso = blahblah
  etc...
  table.insert(tblImages, objImage)
  -- There is no need for assigning nil to variable objImage
  -- objImage is being garbage-collected automatically as it goes out of scope
end

【讨论】:

  • 不幸的是,我不能这样做,因为每个图像都需要通过 for 循环进行多次迭代才能获得完整的数据集。基本上 tblMetadata 是这样的: IMG_123.jpg /n ISO:200 /n etc... /n IMG_124.jpg /n ISO: 800 /n etc...
  • 抱歉,我的伪代码并不完全准确。本质上,我想知道如何以与其他语言相同的方式做到这一点。所以上面的伪代码在这个意义上仍然适用。
猜你喜欢
  • 2018-10-11
  • 1970-01-01
  • 2018-12-17
  • 2013-06-03
  • 2017-07-14
  • 2012-07-02
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多