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

**URL:** <https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416>\
**Category:** General Usage\
**Tags:** while-for-scope, i-not-defined\
**Created:** [July 28, 2021, 6:48am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416 "2021-07-28T06:48:47Z")\
**Posts on this page:** 7\
**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:** [July 28, 2021, 6:48am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/1 "2021-07-28T06:48:47Z")

</div>

I hvave no idea what wrong . Please help!

```julia
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: 

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 28, 2021, 6:54am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/2 "2021-07-28T06:54:02Z")

</div>

Please see:

> [@PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530):
>
> This is a short post on how to use backticks (` and ```) to quote code, so it is easy to read. This post can be linked to new users who are confused by this feature. Why By quoting your code, you get a monospaced font (which preserves indentation) and syntax highlighting. This makes it easier to read your code and help you. Displayed code looks like this: function displayed\_code(x::Int, y::Int) if x \< y println("x is smaller") else println("or not") end end Inline…

and

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 28, 2021, 7:03am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/3 "2021-07-28T07:03:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [July 28, 2021, 7:05am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/4 "2021-07-28T07:05:42Z")

</div>

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

```julia
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`?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [July 28, 2021, 8:05am UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/5 "2021-07-28T08:05:41Z")

</div>

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

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

julia> list1
0.2126448445398994

```

---

<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:** [July 28, 2021, 5:11pm UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/6 "2021-07-28T17:11:29Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [July 28, 2021, 6:00pm UTC](https://discourse.julialang.org/t/i-have-no-idea-whats-wrong-please-help-error-undefvarerror-i-not-defined/65416/7 "2021-07-28T18:00:04Z")

</div>

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
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.
