# @eval in loop error

**URL:** https://discourse.julialang.org/t/eval-in-loop-error/15889
**Category:** General Usage
**Created:** [October 4, 2018, 4:25pm UTC](https://discourse.julialang.org/t/eval-in-loop-error/15889 "2018-10-04T16:25:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [October 4, 2018, 4:25pm UTC](https://discourse.julialang.org/t/eval-in-loop-error/15889/1 "2018-10-04T16:25:43Z")

</div>

Julia 0.7  
loop like below  
in row  
@eval($vx=findnz(cij)[1].+(i-1)\*skok)  
ERROR: UndefVarError: cij  
without loop (when i=j=1 tthis line working !  
var cij is local var and exist  
What wrong ?

```julia
for i=1:length(zakresy).-1
for j=1:length(zakresy).-1
global x,y,z,f
drow=D[:,zakresy[i]+1:zakresy[i+1]]
dcol=D[:,zakresy[j]+1:zakresy[j+1]]
cij=drow'*dcol/k.-mean(drow,dims=1)'*mean(dcol,dims=1)#sek. 
x=string("x",i,"_",j)
vx=Meta.parse(x)
@eval($vx=findnz(cij)[1].+(i-1)*skok)
end
end

```

ERROR: UndefVarError: cij not defined  
Stacktrace:  
[1] top-level scope at none:0  
[2] eval(::Module, ::Any) at .\boot.jl:319  
[3] top-level scope at util.jl:158  
[4] top-level scope at REPL[51

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 4, 2018, 5:06pm UTC](https://discourse.julialang.org/t/eval-in-loop-error/15889/2 "2018-10-04T17:06:59Z")

</div>

`cij` is only defined in the local scope of the loop but `eval` always evaluates in the global scope. Adding a `global` in front of the `cij=...` should do the trick. But IMO you should refactor your code to not need an eval.

Also, you as a veteran should know how to quote code, please do it: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

### Author: ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)
#### Post date: [October 4, 2018, 5:16pm UTC](https://discourse.julialang.org/t/eval-in-loop-error/15889/3 "2018-10-04T17:16:03Z")

</div>

Thanks,  
IS some other way to create/named dynamicly vars in loops ?

v1  
v2  
v3  
?

Paul

W dniu 2018-10-04 o 19:12, Mauro Werder pisze:

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [October 4, 2018, 5:59pm UTC](https://discourse.julialang.org/t/eval-in-loop-error/15889/4 "2018-10-04T17:59:06Z")

</div>

No, but you could use a dict, for instance. Because if you want to access those variables of yours, you will have to do more eval-ing. This is not pretty nor performant.
