I have no idea what's wrong. Please help! ERROR: UndefVarError: i not defined

I hvave no idea what wrong . Please help!

julia> for i=1:length(przedzaly)-1
       var_name=Symbol("lista_polaczona_" * string(litery[i]))
       println(var_name)
       @eval $var_name=lista[przedzaly[i]+1:przedzaly[i+1],:]
       @eval  writedlm("listy_wysylkowe/$var_name.csv",$var_name)
       end
lista_polaczona_A
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval(m::Module, e::Any)
   @ Core .\boot.jl:360
 [3] top-level scope
   @ REPL[23]:4774: 

Please see:

and

2 Likes

Word of advice: never ever use @eval in calculations. If you think that it is impossible to make calculations without @eval it means that your mental model of how Julia works is wrong. There are very good tutorials and docs online, which can give a proper understanding of good Julia code.

4 Likes

In particular, @eval evaluates in global scope, while i is local to your for loop, an MWE would be:

julia> for i = 1:10
           var_name = Symbol("list", i)
           @eval $var_name = rand(10)[i]
       end
ERROR: UndefVarError: i not defined

that said I don’t see why you’re using @eval here at all, as Skoffer says it’s not needed in 99.9% of “regular” Julia usage, and in your case you just seem to index some array (?) by i and then write out the parts you selected by indexing into a csv file, which should be entirely doable without any invocation of @eval?

4 Likes

You can interpolate variables with $ within @eval expressions. With the MWE above:

julia> for i = 1:10
           var_name = Symbol("list", i)
           @eval $var_name = rand(10)[$i]
       end

julia> list1
0.2126448445398994
2 Likes

Yes thanks, but now is Error like below, but without while "for "code is working when I declare “i” .

julia> for i=1:length(przedzaly)-1
       var_name=Symbol("lista_polaczona_" * string(litery[i]))
       println(var_name)
       @eval $var_name=lista[przedzaly[$i]+1:przedzaly[$i+1],:]
       @eval  writedlm("listy_wysylkowe/$var_name.csv",$var_name)
       end
lista_polaczona_A
ERROR: UndefVarError: var_name not defined
Stacktrace:
 [1] top-level scope
   @ none:1
 [2] eval(m::Module, e::Any)
   @ Core .\boot.jl:360
 [3] top-level scope
   @ REPL[24]:5

Could you explain what are you trying to do with eval? It seems that you are trying to generate a variable name programatically but in your example this is not necessary and probably in your program it also isn’t. Are you trying to port a code from maltab?

Why not something like this?

julia> for i=1:length(przedzaly)-1
           file_name= "lista_polaczona_" * string(litery[i])
           println(file_name)
           current_lista = lista[przedzaly[i]+1:przedzaly[i+1],:]
           writedlm("listy_wysylkowe/$(file_name).csv",current_lista)
       end

This uses just a generic variable name “current_lista” in the local scope, whereas the @eval method would create length(przedzaly)-1 global variables with different names.

2 Likes