我是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元數據列表,我可能會在某個時刻。
是這個代碼是否正確?什麼是objTable變量?你能粘貼一些真實的代碼嗎? – pkisztelinski
對不起,應該是objImage。我糾正了它。 – erv