18 Mar 2005 16:40
Re: Need an eval(String ) like function
On 18 Mar 2005, at 15:03, binwbwang wrote:
> Sometimes we need to parse string and get the value.
> e.g.
> assert 5 == eval("2+3") // Python supports this feature
> list = ["ab",1,2,3]
>
> assert "ab" == eval["list[0]"] // using the current variable to
> evaluate the expression, this feature(need more consideration) is an
> optional if it's difficult to implement..
Try in a script the 'evaluate()' method which does what you describe.
In Java land you can simulate this with the GroovyShell
GroovyShell shell = new GroovyShell()
Object answer = shell.evaluate("2 + 3")
James
-------
http://radio.weblogs.com/0112098/
RSS Feed