[英]understanding how a lua function does work?
我有一個使用多個lua腳本的HTML項目,我有一個很大的問題,它隱藏了一個函數的功能(我是lua的新手):
........................
所有要求都已完成,路徑也已定義
local fs = require "lfs"
local const = {}
for num = 1, 14 do
const[num] = assert(
dofile (const_path .. mkfilename(num)),
"Failed to load constant configuration ".. num ..".")
end
local function file_number() --this is the function that causes me a headach
local ci, co, num = ipairs(const)-- when I print num is 0 and ci,co are nil
local vi, vo, _ = fs.dir(virt_path)-- what does _ mean here ?
local function vix(o)
local file = vi(o)
if file == nil then return nil end
local number = file:match("^(%d+).lua$")
if number == nil then return vix(o) end
return tonumber(number)
end
local function iter(o, num)
return ci(o.co, num) or vix(o.vo, num)---where is ci defined or impplemented
end
return iter, {co=co, vo=vo}, num-- what 's the return value here ?
end
功能有效,但我仍然不明白為什么以及如何,我會對任何提示都很滿意。
_
通常是一個拋棄變量。
在這種情況下,雖然它沒有任何意義,可以很容易地完全省略。
ci
應該是一個函數, co
應該是一個表。
同樣的(雖然我不能肯定地說約vo
)的vi
和vo
。
該函數使用迭代器函數構造自己的迭代器,並從ipairs
和fs.dir
函數返回狀態。
行return iter, {co=co, vo=vo}, num
返回一個interator函數,一個狀態表和初始循環變量(初始ipairs
調用中的0
)。
在循環中使用時,將循環遍歷ci
和vix
的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.