It looks like you're new here. If you want to get involved, click one of these buttons!

Can we compute string sums like "1/2" or "40+2" etc directly without a more elaborate string interpreter function?

I tried print( tonumber("1/2 ") ) but that failed as expected...

@Kirl There is a function called load, which loads a function in the form of a string. Example:

function setup() local str="40+3*4/4" --should print out 43 local val=load("return "..str)() print(val) end

Excellent, thanks a lot cc!

@CamelCoder wasn't it called loadstring?

loadstring

a = loadstring("return 63 / 2")() print(a)

Was the name of the function changed? Or am I just wrong?

Edit: I tried both load and loadstring. They both seem to work the same... Can someone please explain to me why. Thanks

load

Both ways work, so either one can be used in this case.

@Kolosso As @dave1707 said, both work. I used to use loadstring(), but then I realized that it would be simpler to use load() because it is actually in the auto-complete.

@dave1707 @CamelCoder thanks!

## Comments

@Kirl There is a function called load, which loads a function in the form of a string.

Example:

Excellent, thanks a lot cc!

@CamelCoder wasn't it called

`loadstring`

?Was the name of the function changed? Or am I just wrong?

Edit: I tried both

`load`

and`loadstring`

. They both seem to work the same... Can someone please explain to me why. ThanksBoth ways work, so either one can be used in this case.

@Kolosso As @dave1707 said, both work. I used to use loadstring(), but then I realized that it would be simpler to use load() because it is actually in the auto-complete.

@dave1707 @CamelCoder thanks!