2017-06-17 56 views
1

我是Lua新手,我剛剛嘗試過在其他語言中做一些常見而直接的事情,但是在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中,它將剛剛添加到表中的對象設置爲零。以這種迭代方式將一系列「對象」添加到表中的技術是什麼?我嘗試使用第二個objImage(objImage2),並在將它添加到表(objImage2)之前將objImage分配給它,但它只是將指針/引用分配給原始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元數據列表,我可能會在某個時刻。

+0

是這個代碼是否正確?什麼是objTable變量?你能粘貼一些真實的代碼嗎? – pkisztelinski

+0

對不起,應該是objImage。我糾正了它。 – erv

回答

3

可能您在使用空表,而不是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 
+0

啊,我確信我已經嘗試過了。是的,這解決了它。謝謝。 – erv

+1

這將使地方更清晰地移動循環內的objImage的聲明。這樣你就不需要最終的任務。您甚至可以使用表構造函數填充表的兩個字段。然後你可以內聯表構造器,這樣循環體只有一個聲明。 'table.insert(tblImages,{name = v,iso = v})' –

+0

查看我的回覆Egor的回答,@TomBlodget – erv

1
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 
+0

不幸的是,我不能這樣做,因爲每個Image都需要通過for循環進行多次迭代才能獲得完整的數據集。基本上tblMetadata是這樣的: IMG_123.jpg/N ISO:200/N 等../N IMG_124.jpg/N ISO:800/N 等等 – erv

+0

對不起,我的僞代碼ISN沒有完全準確。基本上我想知道如何以與其他語言如何完成相同的方式來完成此操作。所以上面的僞代碼在這個意義上仍然適用。 – erv