It is possible. You can use to *parse* [1] to convert a string into an 
*Expr* [2] object. After that you can recursively walk over the parsed 
expression and check whether each symbol is defined using *isdefined* [3]. 
Something like the following might cover what you need:

vars(vs, _) = vs
vars(vs, s::Symbol) = isdefined(s) ? vs : push!(vs, s)
function vars(vs, e::Expr)
    for arg in e.args
        vars(vs, arg)
    end
    vs
end

extractvars(s::String) = vars(Set{Symbol}(), parse(s))

extractvars("3*x+cos(z)")

Changing *Set{Symbol}()* to* Symbol[]* would give you an array like you 
wanted, but might contain duplicates. Using a set avoids having to deal 
with that explicitly. Hopefully that gets you started.

[1] 
http://docs.julialang.org/en/latest/stdlib/base/?highlight=parse#Base.parse
[2] http://docs.julialang.org/en/latest/manual/metaprogramming
[3] 
http://docs.julialang.org/en/latest/stdlib/base/?highlight=isdefined#Base.isdefined

On Tuesday, 6 May 2014 21:23:29 UTC+2, El Afrit Mariem wrote:
>
> Hi,
>
> Is it possible to extract the list of variables from an expression?
>
> Example:
>
> expr = "3*x+cos(z)"
>
> and I want to have as a result this list : [x,z]
>
> Thank you
>

Reply via email to