# Variable Scoping in Non-Interactive Context

**URL:** <https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603>\
**Category:** General Usage\
**Tags:** scope, for-loop\
**Created:** [June 13, 2024, 2:20pm UTC](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603 "2024-06-13T14:20:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![astro-kevin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astro-kevin/32/43342_2.png) [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Post date:** [June 13, 2024, 2:20pm UTC](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603/1 "2024-06-13T14:20:41Z")

</div>

I’m trying to index a vector in a soft local variable scope, but I just can’t seem to do it. The code looks minimally something like this:

in a file called `testfile.jl`

```julia
struct TestStruct
    thing::Int64
end
function Foo()
    a = Vector{TestStruct}(undef,10)
    fill!(a, TestStruct(0)
    for i in 1:10
        a[i] = TestStruct(i^2)
    end
    return a
end

```

then, in a separate file called `test.jl`

```julia
include("testfile.jl")

println(Foo())

```

This gives the expected result, but in my proper code which is quite a bit more complicated doesn’t. In that instance (using assert statements to determine the issue), `a` is being returned as an array of TestStruct(0)'s, as if the for loop didn’t do anything. I think this is a scoping issue since if you check the value within the for loop, it gives the correct answer, but if you check the value out of the for loop, it doesn’t. However, from what I can tell, I’m writing the scoping correctly by wrapping it in a function, and given that the example here works as expected, I don’t know what could possibly be the issue.

Does anyone have any ideas on what could cause this, or otherwise what I can try to further diagnose it?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 13, 2024, 2:53pm UTC](https://discourse.julialang.org/t/variable-scoping-in-non-interactive-context/115603/2 "2024-06-13T14:53:56Z")

</div>

> [@astro-kevin](#):
>
> otherwise what I can try to further diagnose it?

Best to come up with a minimal example that actually reproduces your difficulty. (Take your real code and keep deleting things a little at a time.)
