It always helps to form a minimal reproducible example. Get rid of all unnecessary lines so you can focus on the underlying issue.
Here’s one example:
julia> function test()
for i in 1:2
local item
if i == 1
item = [i]
else
item[] += 1
end
end
end
test (generic function with 2 methods)
julia> test()
ERROR: UndefVarError: `item` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
One the first iteration, you declare local item and initialise it.
On the second iteration, you declare a new local item, and then attempt item[] += 1, which tries to read item[]. But because of local item, the variable does not persist between iterations and so it doesn’t exist in iteration 2.
Change where you put the local:
julia> function test()
local item
for i in 1:2
if i == 1
item = [i]
else
item[] += 1
end
end
return item
end
test (generic function with 2 methods)
julia> test()
1-element Vector{Int64}:
2