Lua功能 – 一个简单的误解
发布时间:2020-12-14 21:48:24 所属栏目:大数据 来源:网络整理
导读:我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算: 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 endprint (Result(a))error: attempt t
我正在尝试开发一个函数,它对两个具有相同键的值执行数学运算:
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) 问题是该函数在字面上尝试数学 我能够调用函数(x)返回x.something结束,但如果我尝试函数(x)something.x我得到一个错误.我需要提高我对Lua中函数的理解,但我在手册中找不到这个. 解决方法
我不确定你要做什么,但这里有一些基于你的代码的工作代码:
property = {a=120,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′ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |