2016-12-22 16 views
2

文字列をExprに変換する方法はありますか?私は、次のことを試してみましたが、それは動作しません:文字列をevalする/文字列をExprに変換する方法

julia> convert(Expr, "a=2") 
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Expr 
This may have arisen from a call to the constructor Expr(...), 
since type constructors fall back to convert methods. 

julia> Expr("a=2") 
ERROR: TypeError: Expr: expected Symbol, got String 
in Expr(::Any) at ./boot.jl:279 
+6

私はしたいと思います 'パース( "A = 2")' –

+0

をありがとう(それは文字列の内容に応じて、Symbol'または '' Expr'に変換されます)。それは私が必要なものです。私はExprの中間でドキュメント(http://docs.julialang.org/en/release-0.5/manual/metaprogramming/)をスキミングしましたが、 '' parse''がトップセクションにあることを確認していませんでした。ページ。 – Phuoc

答えて

3

コリンが言ったように、Expr(またはSymbol)に変換するために、あなたはparseを使用しています。 そして、結果として得られるExprを評価するには、evalを使用します。 どちらも一緒:

julia> eval(parse("a = 2")) 
2 
関連する問題