Home > Software engineering >  How to assign string to a function in lua
How to assign string to a function in lua

Time:03-31

I am writing a calculator based on the script I have in Python and for the arithmatic operators I used:

operations={'ADD':add,'PLUS':add,'SUM':add,'ADDITION':add,
            'SUB':sub,'SUBTRACT':sub, 'MINUS':sub,
            'DIFFERENCE':sub,'LCM':lcm,'HCF':hcf,'PRODUCT':mul,
            'MULTIPLY':mul,'MULTIPLICATION':mul,
            'DIVISION':div,'MOD':mod,'REMAINDER':mod,'MODULAS':mod}

This was the python one but the same idea does not work for Lua. In the example the format goes: {'UserInput':Function} Any help would be appreciated.

I tried replacing : with -> and = but none of them worked.

CodePudding user response:

The equivalent of this python code:

t = {"foo": "bar", "one": "two"}

t["foo"]

in lua is:

t = {["foo"] = "bar", ["one"] = "two"}

t["foo"]

or:

t = {["foo"] = "bar", ["one"] = "two"}

t.foo
  • Related