我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算: property = {a=120, b=50, c=85}operator = {has = {a, b}, coefficient = {a = 0.45}}function Result(x) return operator.has.x * operator.coefficient.x endp
property = {a=120, b=50, c=85} operator = {has = {a, b}, coefficient = {a = 0.45}} function Result(x) return operator.has.x * operator.coefficient.x end print (Result(a)) error: attempt to perform arithmetic on field 'x' (a nil value)
问题是该函数在字面上尝试数学
“operator.has.x”而不是“operator.has.a”.
我能够调用函数(x)返回x.something结束,但如果我尝试函数(x)something.x我得到一个错误.我需要提高我对Lua中函数的理解,但我在手册中找不到这个.
我不确定你要做什么,但这里有一些基于你的代码的工作代码:property = {a=120, b=50, c=85} operator = {has = {a=2, b=3}, coefficient = {a = 0.45}} function Result(x) return operator.has[x] * operator.coefficient[x] end print (Result('a'))
打印’0.9′