Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua functions -- a simple misunderstanding

I'm trying to develop a function which performs math on two values which have the same key:

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)

The problem is that the function is attempting math on literally "operator.has.x" instead of "operator.has.a".

I'm able to call a function (x) return x.something end, but if I try function (x) something.x i get an error. I need to improve my understanding of functions in Lua, but I can't find this in the manuals.

like image 974
ridthyself Avatar asked Jan 14 '23 08:01

ridthyself


1 Answers

I'm not exactly sure what you're trying to do, but here is some working code that is based on your code:

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'))

Prints '0.9'

like image 89
Michael Geary Avatar answered Jan 22 '23 20:01

Michael Geary