【发布时间】:2015-02-05 02:08:36
【问题描述】:
使用 io.close() 之类的方法,您可以像这样使用它:
file:close()
有没有办法创建一个像这样工作的自定义函数,你可以在变量上调用它?
对我来说,我试图通过使用 string.find 来查找空格,从而将参数与文本文件分开
所以在文本文件中看起来像
this is some input
并且 readArgs() 函数应该返回表中的整行,其中 args[1] = "So", args[2] = "in", args[3] = "the" 等在被调用后线
function readFile(file)
local lines = {}
assert(io.open(file), "Invalid or missing file")
local f = io.open(file)
for line in f:lines() do
lines[#lines+1] = line
end
return lines
end
function readArgs(line) -- This is the function. Preferably call it on the string line
--Some code here
end
【问题讨论】:
-
所以,澄清一下,你想做的是能够打电话给
someStringVariable:readArgs()? -
是的。也许在 string:readArgs() 之类的东西上调用它,然后让它返回一个参数表或其他东西
-
术语:函数有值作为参数;不是变量。
:的 LHS 是一个表达式。