@eval in loop error

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 ?

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

1 Like

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

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:

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.

1 Like