Lua:有没有办法将“nil”连接起来?价值观?
我在 Lua 中有以下函数:
function iffunc(k,str,str1)
if k ~= 0 then
return str .. k .. (str1 or "")
end
end
该函数允许我检查值 k 是否已填充。我实际上用它来确定是否要显示零值的内容。我的问题是这样的:我试图连接 iffunc() 的字符串,但由于某些值是 0,它返回尝试连接 nil 值的错误。例如:
levellbon = iffunc(levellrep["BonusStr"],"@wStr@r{@x111","@r}") .. iffunc(levellrep["BonusInt"],"@wInt@r{@x111","@r}") .. iffunc(levellrep["BonusWis"],"@wWis@r{@x111","@r}")
如果任何表值为 0,则会返回错误。我可以轻松地将“return 0”放入 iffunc 本身;但是,我也不想要一串 000。那么,无论哪些值为零,我该如何处理它,我都不会收到该错误呢?最终,我将对 levellbon 变量执行 iffunc 来查看它是否已填充,但我已经弄清楚了该部分。我现在只需要克服这个小障碍。谢谢!
I have the following function in Lua:
function iffunc(k,str,str1)
if k ~= 0 then
return str .. k .. (str1 or "")
end
end
This function allows me to check if value k is populated or not. I'm actually using it to determine if I want to display something that has zero value. My problem is this: I'm trying to concatenate a string of iffunc(), but since some of the values are 0, it returns an error of trying to concatenate a nil value. For instance:
levellbon = iffunc(levellrep["BonusStr"],"@wStr@r{@x111","@r}") .. iffunc(levellrep["BonusInt"],"@wInt@r{@x111","@r}") .. iffunc(levellrep["BonusWis"],"@wWis@r{@x111","@r}")
If any of the table values are 0, it'll return the error. I could easily put 'return 0' in the iffunc itself; however, I don't want a string of 000, either. So how can I work it where no matter which values are nil, I won't get that error? Ultimately, I'm going to do an iffunc on the levellbon variable to see if that's populated or not, but I've got that part figured out. I just need to get past this little hurdle right now. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会这样做:
I'd do this:
您应该在函数中添加一个
else
语句,在其中返回一个空字符串 (""
)。You should add an
else
statement in the function, where you return an empty string (""
).