# Cannot find var's name when I eval an expr

**URL:** <https://discourse.julialang.org/t/cannot-find-vars-name-when-i-eval-an-expr/33460>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 16, 2020, 10:04pm UTC](https://discourse.julialang.org/t/cannot-find-vars-name-when-i-eval-an-expr/33460 "2020-01-16T22:04:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang/32/12339_2.png) [@Fang](https://discourse.julialang.org/u/Fang)\
**Post date:** [January 16, 2020, 10:04pm UTC](https://discourse.julialang.org/t/cannot-find-vars-name-when-i-eval-an-expr/33460/1 "2020-01-16T22:04:27Z")

</div>

Hi

I wanna use eval an expr but the error says that the var’s name cannot be found.  
My function is below :

```julia
function cal(jac, rate, Q)
    n_spec = size(jac, 1)

    jac_value = zeros(Float64, n_spec, n_spec)
    for i = 1:n_spec, j=1:n_spec
        expr = Meta.parse(jac[i, j])
        dump(expr)
        Core.eval(Main, expr)
        println(expr.args)
    end

    return jac_value
end

```

and one of my expr is :(1 \* -1 \* rate[7] + 1 \* -1 \* rate[1] \* Q[2] ^ 1 + 0)  
and dump result is

```julia
Expr
  head: Symbol call
  args: Array{Any}((4,))
    1: Symbol +
    2: Expr
      head: Symbol call
      args: Array{Any}((4,))
        1: Symbol *
        2: Int64 1
        3: Int64 -1
        4: Expr
          head: Symbol ref
          args: Array{Any}((2,))
            1: Symbol rate
            2: Int64 7
    3: Expr
      head: Symbol call
      args: Array{Any}((5,))
        1: Symbol *
        2: Int64 1
        3: Int64 -1
        4: Expr
          head: Symbol ref
          args: Array{Any}((2,))
            1: Symbol rate
            2: Int64 1
        5: Expr
          head: Symbol call
          args: Array{Any}((3,))
            1: Symbol ^
            2: Expr
              head: Symbol ref
              args: Array{Any}((2,))
                1: Symbol Q
                2: Int64 2
            3: Int64 1
    4: Int64 0

```

but it raised an error shows that `ERROR: LoadError: UndefVarError: rate not defined`  
I have tried many ways, but I don’t know how to solve it. Thanks for helping.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [January 16, 2020, 10:19pm UTC](https://discourse.julialang.org/t/cannot-find-vars-name-when-i-eval-an-expr/33460/2 "2020-01-16T22:19:10Z")

</div>

`eval` works at global scope, so it cannot create local variables inside a function, no matter what you do. There’s some related discussion in [Unpack named tuples (does anyone know how to do it?) - #9 by rdeits](https://discourse.julialang.org/t/unpack-named-tuples-does-anyone-know-how-to-do-it/32456/9) .

Can you pass an anonymous function instead of an expression? That should be more flexible and _much_ more efficient.

---

<div class="post-metadata">

**Author:** ![Fang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fang/32/12339_2.png) [@Fang](https://discourse.julialang.org/u/Fang)\
**Post date:** [January 17, 2020, 4:19am UTC](https://discourse.julialang.org/t/cannot-find-vars-name-when-i-eval-an-expr/33460/3 "2020-01-17T04:19:44Z")

</div>

Thanks for your reply. May I ask about if I would like to use anonymous function just like you said, is there something like this in Julia?

```julia
# something above
rate = zeros(Float64, 10, 10)
f(rate) = rate[10, 1] + 1
# read some new file
# Is there something like this in Julia?
f(rate) = f(rate) + rate[5, 2]
# which can append the function

```

or can I generate an anonymous function from a string?
