# Unconsistent range object behavior

**URL:** https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351
**Category:** General Usage
**Created:** [April 20, 2019, 4:14pm UTC](https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351 "2019-04-20T16:14:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wizebt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wizebt/32/12256_2.png) [@wizebt](https://discourse.julialang.org/u/wizebt)
#### Post date: [April 20, 2019, 4:14pm UTC](https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351/1 "2019-04-20T16:14:13Z")

</div>

New to Julia migrating my code from Octave I encountered a very strange error. Same piece of code works fine when sequential

julia\> include(“test.jl”)  
a[y,x][8 9 6 6 4; 4 7 2 5 3; 5 6 9 2 1; 5 3 4 3 3; 7 8 6 2 3]  
2:6 3:7  
r 4.859504132231405 c 3.628099173553719  
2:6 3:7  
r 4.859504132231405 c 3.628099173553719

but strange enough doesn’t work anymore in a loop, the range object is not recognized.

julia\> include(“test.jl”)  
a[y,x][4 5 3 1 9; 8 9 9 4 8; 1 7 1 5 3; 7 3 7 3 4; 1 4 1 6 7]  
ERROR: LoadError: UndefVarError: x not defined  
Stacktrace:  
[1] top-level scope at /home/manu/code/julia/test.jl:15 [inlined]  
[2] top-level scope at ./none:0  
[3] include at ./boot.jl:317 [inlined]  
[4] include\_relative(::Module, ::String) at ./loading.jl:1041  
[5] include(::Module, ::String) at ./sysimg.jl:29  
[6] include(::String) at ./client.jl:388  
[7] top-level scope at none:0  
in expression starting at /home/manu/code/julia/test.jl:14

See below the code.  
Uncomment the for loop and you get an error  
`  
function cog(x,y,J)  
I=J[y,x]  
sI = sum(I)  
c = sum(x.\*vec(sum(I,dims=1)))/sI  
r = sum(y.\*vec(sum(I,dims=2)))/sI  
return r,c  
end

a=rand(1:9,12,8)  
r,c,d=5,4,2  
x,y=c-d:c+d,r-d:r+d  
println(“a[y,x]”,a[y,x])

#for i=1:2  
println(x," ",y)  
r,c=cog(x,y,a)  
println("r “,r,” c “,c)  
c=round(Int,c)  
r=round(Int,r)  
x,y=c-d:c+d,r-d:r+d  
println(x,” ",y)  
r,c=cog(x,y,a)  
println("r “,r,” c ",c)  
#end`

---

<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: [April 20, 2019, 4:24pm UTC](https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351/2 "2019-04-20T16:24:43Z")

</div>

Loops in Julia create their own local scope. If you want to access a global variable you have to either declare it `global` in your loop or wrap your code in a function. There’s loads of threads on this on here if you search for the error.

---

<div class="post-metadata">

### Author: ![wizebt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wizebt/32/12256_2.png) [@wizebt](https://discourse.julialang.org/u/wizebt)
#### Post date: [April 20, 2019, 6:56pm UTC](https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351/3 "2019-04-20T18:56:34Z")

</div>

Thanks for you rapid answer. This variable scope in loops is surprisingly new for me… I should RTFM 🙂  
Pb solved

---

<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: [April 20, 2019, 7:53pm UTC](https://discourse.julialang.org/t/unconsistent-range-object-behavior/23351/4 "2019-04-20T19:53:43Z")

</div>

Also, please quote your code in posts: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)
