# Evaluation of string with local variable

**URL:** <https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713>\
**Category:** General Usage\
**Created:** [July 28, 2018, 9:40pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713 "2018-07-28T21:40:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [July 28, 2018, 9:40pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/1 "2018-07-28T21:40:08Z")

</div>

I have strings in context of symbolic diffferentiation. Therefore I need to evaluate strings which contain local variables.  
In the manual to Metaprogramming is this example:

```julia
macro zerox()
  return esc(:(x = 0))
end

function foo()
  x = 1
  @zerox
  x # is zero
end

```

But why doesn’t work that version?

```julia
macro zerox(string)
    return esc(string)
end

function foo()
    x = 1
    b = parse("x = 0")
    @zerox(b)
    x # is now one
end

```

I really appreciate your help!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 28, 2018, 9:50pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/2 "2018-07-28T21:50:36Z")

</div>

No it’s impossible and metaprogramming will never help.

To quote myself in a [recent post](https://discourse.julialang.org/t/parse-string-to-datatype/7118/12):

> Many people seems to think macro gives you magic power to accomplish things at runtime that are not possible without them. They don’t, they are just a fancy way to save repeated typing.

---

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [July 28, 2018, 9:59pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/3 "2018-07-28T21:59:46Z")

</div>

Okay, that’s important to know! Then I will just ask the other way round:  
I have given formulas for nonlinear equations in a txt. document. I rearrange and differentiate them automatically so that I can calculate residuals as well as the Jacobian for the `nlsolve()` function from the NLsolve package (still in string format). My plan was to parse and evaluate the strings repeatedly in the `f!()` and `j!()` function as I have to solve these formulas many times with different parameters depending on the previous solution. How would you approach that?  
EDIT: It works more or less with global variables but first it is slow and second it is quite uncomfortable.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 28, 2018, 10:03pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/4 "2018-07-28T22:03:29Z")

</div>

You should construct functions from those expressions and pass variables they need to reference as arguments. You have to use `eval` of it’s variances to construct the function and you have to use `invokelatest` to call the function after you are done with that.

If you only need to read a text file though, it should be much better to just make that text file a julia file instead and pass the formulas in as functions directly.

---

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [July 28, 2018, 10:05pm UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/5 "2018-07-28T22:05:05Z")

</div>

Thanks a lot! I will try that out…

---

<div class="post-metadata">

**Author:** ![economyboy](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@economyboy](https://discourse.julialang.org/u/economyboy)\
**Post date:** [August 8, 2018, 10:55am UTC](https://discourse.julialang.org/t/evaluation-of-string-with-local-variable/12713/6 "2018-08-08T10:55:16Z")

</div>

Hey, I followed your advice and it works perfectly. Thanks!
