# Why I don't obtain an output variable in a for loop?

**URL:** <https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794>\
**Category:** General Usage\
**Created:** [June 3, 2023, 12:18am UTC](https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794 "2023-06-03T00:18:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RDuarte](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Post date:** [June 3, 2023, 12:18am UTC](https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794/1 "2023-06-03T00:18:58Z")

</div>

Hello

I’m struggling with a for loop in julia.  
My WAVFilesList is a vector of five strings.  
I don’t understand why when I’m doing the following command I do not obtain a WAVfilenames variable in each passage of the loop. WAVfilenames simply do not exist.

for i in 1:length(WAVFilesList)  
WAVfilenames=WAVFilesList[i];  
end

However, If I copy-paste just that line of code (WAVfilenames=WAVFilesList[i]) and change i for a number I can obtain WAVfilenames variable.

What am I missing here?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 3, 2023, 12:39am UTC](https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794/2 "2023-06-03T00:39:07Z")

</div>

It may be a little long, but [https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables) should explain it. In the documentation in this link, you can find:

```julia
julia> for i = 1:3
           x = "hello" # new local
           println(x)
       end
hello
hello
hello

julia> x
ERROR: UndefVarError: `x` not defined

```

which is like the example you gave, and is the result of **soft scoping** , explained in the linked manual page.

---

<div class="post-metadata">

**Author:** ![RDuarte](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Post date:** [June 3, 2023, 2:04pm UTC](https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794/3 "2023-06-03T14:04:57Z")

</div>

Hello @Dan ,  
I read it already and I defined the variable outside the loop, however I still have the same problem.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![RDuarte](https://avatars.discourse-cdn.com/v4/letter/r/a183cd/32.png) [@RDuarte](https://discourse.julialang.org/u/RDuarte)\
**Post date:** [June 3, 2023, 2:16pm UTC](https://discourse.julialang.org/t/why-i-dont-obtain-an-output-variable-in-a-for-loop/99794/4 "2023-06-03T14:16:07Z")

</div>

I guess I solved it.  
I just add: global behind the name of my variable and it seems its working.  
Thank you
