ID:273107
 
Say I want users to input complicated math in the form of a string

"3+5+3+2x3", and i want it to be handled ingame, at runtime. Meaning [3+5+3+2x3] won't work. Is there a library that lets you do, say...

mob/verb/MathProblem(var/T="3+3+3" as text)
usr << "The answer to [T] is : [doMath(T)] !"

[SOLVED: http://www.byond.com/developer/AbyssDragon/SET ]
No but there's a proc called text2num().
In response to Andre-g1
That just will not work. I'm including a 'scripting' option in my game for hosts to customize their servers, and a 'domath' option would be A BIG HELP

text2num() does not work as it does not do the math contained in the string. It only returns the first number found in the string. (ex. 3+3+3 returns 3, not 9)

I'd highly appreciate a library that is capable of converting strings to math and calculating a result.
You need an expression evaluator for stuff like that. I'm pretty sure there are some libraries around that have expression evaluators.

Though if you want to make one yourself, there's nothing stopping you. It may be kind of difficult for inexperienced programmers, though.

[EDIT]: Ah I found one. http://www.byond.com/developer/AbyssDragon/SET
In response to D4RK3 54B3R
Thanks, you're a life saver man!

http://www.byond.com/developer/AbyssDragon/SET is exactly what i'm looking for

Wow...just, wow!

I was searching for the word 'math' in the resources, pulled up nothing related. Never thought of trying 'expression'!

Many thanks
mob/verb/evaluate(T as text)
usr << eval(T) // So "1+2+3" is 6
In response to Nadrew
Of course, you should at least leave a note or short-explanation about it, rather than just throw it at him. Ah, I know, you must've left that for me! =P


eval() is a built-in undocumented proc that... evaluates expressions. The expressions are of course given in text string form, and the syntax used in them is similar to DM's own*.
This proc is undocumented for good reason: although it is implemented into the language, it was never completed. Therefore, this proc isn't completely functional, and can even be potentially buggy[link]. So a presumably-polished library, like SET, should be much more functional and complete in comparison (albeit unfortunately inevitably slower). Note that at the same time, I've read eval() is good with basic arithmetic functions (i.e. + - / *). If you will, forum search for more info and testimony.

*: What eval() was designed to parse isn't only simple arithmetic statements, but full DM Script: a subset language included in DM, used in .DMS files or the client/script variable. It can be used in either client-side or server-side scripts, for things such as style (CSS) control and macro definitions. Nowadays, DM Script is less recommended because it is now outdated on most fronts (mainly by version 4.0's interface functions), though it can still be used.
DM Script is a little similar to JavaScript; you can declare vars in it and whatnot, and eval() supports that as well example: [link], however again, unfortunately eval() is incomplete so it can't properly parse full DM Script (read the above linked post and forum search for more info).

P.S. @Nadrew: We don't happen to have any DM Wiki (such as yours) still up or something, right? As is evident, occasionally I (and others) get the knack to document or explain things, and posts such as this one are well-suitable to be incorporated in such a repository. Maybe we should try to make something like that work again, though I won't go deep into that here.
In response to Kaioken
Trying to look up eval(), but of course it's not documented. Does it return a string or an integer?

And as long as it can handle expressions like 2(3*4)+2, i should be happy with it
In response to Nadrew
I actually wouldn't recommend eval(). It's undocumented for some good reasons, including that until a while back it even had a pretty serious bug. An expression parser like SET is definitely the better option.

Lummox JR