Lua如何创建可用于变量的自定义函数? [英] Lua How to create custom function that can be used on variables?
问题描述
file:close()
有没有一种方法可以创建一个类似于此的自定义函数,可以在变量上调用它?
对于我来说,我试图用它通过使用string.find来查找空格来分离文本文件中的参数。
因此,在文本文件中,它看起来像
这是一些输入
readArgs()函数应该返回表中的整个行,其中args [1] =So,args [2] =in ,args [3] =the等被调用后行
函数readFile(file)
本地行= {}
断言(io.open(文件),文件无效或缺失)
local f = io.open(file)
for line in lines:做
行[#行+ 1] =行
结束
返回行
结束
函数readArgs(行) - 这是函数。最好在字符串行上调用它
- 这里的一些代码
结束
根据您的描述,您听起来像是在类似于以下语法的内容之后:
local line = readFile(file)
lines:readArgs(1) - 解析第一行{this,is,some,input}
Metatables可以解决这个问题:
local mt = {__index = {}}
函数mt .__ index.readArgs(self,linenum)
if self [linenum]然后返回nil结束
local args = {}
for each in self [linenum]:gmatch[^] +do
table.insert(args,each)
end
return args
end
您必须对 readFile
并将该metatable附加到您要返回的行
:
函数readFile(文件)
- ...
返回setmetatable(行s,mt)
结束
编辑:回答OP的评论,像这样的调用:
pre $ lines:readArgs(1)
仅仅是语法糖:
lines.readArgs (行,1)
当lua虚拟机执行上述行时,会发生以下情况:
- 表
行
是否有readArgs
key? - 如果是,则照常使用其相应的值作为语句的其余部分。 c>行有一个metatable .__索引?在这种情况下,它会这样做,因此使用分配给
__ index.readArgs
的函数。
readArgs
self
= lines,linenum
= 1
这里没有什么特别的 self
,它只是一个常规参数;你可以将它命名为任何你想要的东西。
With methods like io.close(), you can use it like this:
file:close()
Is there a way to create a custom function that works like that, where you can call it on a variable?
For me I am trying to use it to separate arguments from a text file by using string.find to find spaces
So in the text file it looks like
this is some input
And the readArgs() function should return the entire line in a table with args[1] = "So", args[2] = "in", args[3] = "the" etc. after being called on the line
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
Based on your description it sounds like you're after something similar to this syntax:
local lines = readFile(file)
lines:readArgs(1) -- parse first line {"this", "is", "some", "input"}
Metatables can help with this:
local mt = { __index = {} }
function mt.__index.readArgs(self, linenum)
if not self[linenum] then return nil end
local args = {}
for each in self[linenum]:gmatch "[^ ]+" do
table.insert(args, each)
end
return args
end
You'll have to do a slight change in your readFile
and attach that metatable to the lines
you're returning:
function readFile(file)
-- ...
return setmetatable(lines, mt)
end
Edit: To answer the OP's comment, a call like this:
lines:readArgs(1)
is just syntactic sugar for:
lines.readArgs(lines, 1)
When the lua VM executes the above line the following happens:
- Does the table
lines
have areadArgs
key? - If yes then use its corresponding value as usual for the remaining part of the statement.
- If no, does
lines
have a metatable.__index? In this case it does, so the function assigned to__index.readArgs
is used. readArgs
is now called with the parameters above:self
= lines,linenum
= 1
There is nothing special about self
here, it's just a regular parameter; you can name it anything you want really.
这篇关于Lua如何创建可用于变量的自定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!