# I keep getting this error--- syntax: invalid iteration specification

**URL:** <https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357>\
**Category:** New to Julia\
**Created:** [April 11, 2022, 8:27pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357 "2022-04-11T20:27:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 11, 2022, 8:27pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/1 "2022-04-11T20:27:47Z")

</div>

I am trying to run this code in code in Julia but I keep getting this error which am struggling to correct. syntax: invalid iteration specification

Any help will be much appreciated please. Below is my code:

```julia
x = [0, 5, -5, 4, -5] # x cordinates of the circle
y = [0, 0.6, 0.5, -5, -4.5] # y cordinates of the circle
 
coverage=[[1, 0, 0, 0, 1],[0, 0, 0, 1, 1],[1, 0, 0, 0, 1],[0, 0, 1, 1, 1],[0, 0, 0, 1, 1], [0, 1, 0, 0, 1],[0, 0, 1, 1, 0],[0, 0, 0, 1, 0],[1, 0, 0, 0, 0],[1, 1, 0, 0, 0],[0, 1, 0, 0, 0],[0, 0, 1, 0, 0],[0, 0, 1, 0, 0],[1, 0, 0, 0, 0],[0, 0, 0, 1, 0]]
#A=[[2, 3, 4], [3, 2, 5]]
a=[-1, -1, 1.2, 1.2, 1.2, 0.8, 2.5, 2.5, -3, -3, -1.5, 3, 0.9, -3.7, 3.7] #x cordinate of the points
b=[0.2, 0.8, -0.85, 1.2, 0.5, -1.5, -1.3, 1.4, 1.5, -1.3, -0.8, -2.7, -2.4, 0.5, 0.8] #y cordinate of the points
r=[] # array to store max distances for the center of each circle to each point
#print("A =", A)
print("A =", coverage)

function distance()
    for i,xv in enumerate(x):
        R=[] # store the distance of each circle i to each point
        for j, jv in enumerate(a):
            if (coverage[j][i]==1):
               R.append( ((x[i]-a[j])**2 + (y[i]-b[j])**2)**0.5)
        r.append(max(R)) # find maximum distance from circle i to each point and save in r 
           # end
    return r
    
distance()

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 11, 2022, 9:34pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/2 "2022-04-11T21:34:41Z")

</div>

Hi, this looks like a badly formatted incomplete translation from Python to Julia. Would you be so kind as to use triple back ticks to mark down your code?

With regard your question: putting your tuples into brackets like

```julia
    for (i, xv) in enumerate(x)

```

should help.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 12, 2022, 6:39am UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/3 "2022-04-12T06:39:01Z")

</div>

it’s not valid Julia code  
it should be something like this

```julia

function distance(x, y, a, b) 
    r = Float64[]
    for (i,xv) in enumerate(x)
        R = Float64[]
        for (j, jv) in enumerate(a)
            if coverage[j][i] == 1
                push!(R, ((x[i]-a[j])^2 + (y[i]-b[j])^2)^0.5)
            end
            if length(R) > 0
                push!(r, maximum(R))
            end
         end
    end
    return r
end

```

Although that is still not optimal because of the allocation of R and pushing to r inside the loop.

I believe you should calculate the values for R and extract the r separately.

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 12, 2022, 3:25pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/4 "2022-04-12T15:25:39Z")

</div>

Thank you very much. I tried that but had this displaying now instead of the output I desire. It reads:

distance (generic function with 2 methods)

Can you please help me correct this in other to display the values of r?

thank you

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 12, 2022, 3:42pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/5 "2022-04-12T15:42:52Z")

</div>

you have to call the function

```julia
distance(x, y, a, b) 

```

which will display the returned r

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 12, 2022, 4:30pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/6 "2022-04-12T16:30:41Z")

</div>

Thank you

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 12, 2022, 4:33pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/7 "2022-04-12T16:33:09Z")

</div>

I accidentally missed out passing `coverage` as an argument

But I hope you can get to grips with it all

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 12, 2022, 4:36pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/8 "2022-04-12T16:36:01Z")

</div>

I was trying to figure that out.  
Where should I put that in my code?

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 12, 2022, 4:37pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/9 "2022-04-12T16:37:52Z")

</div>

I am very new to Julia. The syntax of python sometimes confuses me with Julia. I hope to improve gradually in the near future. Thank you very much for your help.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 12, 2022, 4:37pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/10 "2022-04-12T16:37:56Z")

</div>

just as another argument

```julia
function distance(x, y, a, b, coverage)

```

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 12, 2022, 8:01pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/11 "2022-04-12T20:01:52Z")

</div>

Thank you very much. This works perfectly now but giving me all the distances calculated for R. I want to take the maximum value of each of this distances stored in “R” and save it “r”. I am expecting only 5 outputs in all. Is there any way I can achieve this?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [April 13, 2022, 6:26am UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/12 "2022-04-13T06:26:17Z")

</div>

Seems we ran into the problem of using significant whitespace so appending to r ended up inside the inner loop.

Here I have moved it outside.

```julia
function distance(x, y, a, b, coverage) 
    r = Float64[]
    for (i,xv) in enumerate(x)
        R = Float64[]
        for (j, jv) in enumerate(a)
            if coverage[j][i] == 1
                push!(R, ((x[i]-a[j])^2 + (y[i]-b[j])^2)^0.5)
            end
         end
         if length(R) > 0
             push!(r, maximum(R))
         end
    end
    return r
end

```

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [April 13, 2022, 12:37pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/13 "2022-04-13T12:37:12Z")

</div>

> [@lawless-m](#):
>
> problem of using significant whitespace

You mean as a visual indicator, right? The fault was a misplaced `end`, the indentation is luckily irrelevant here 😃

---

<div class="post-metadata">

**Author:** ![Eric.O](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@Eric.O](https://discourse.julialang.org/u/Eric.O)\
**Post date:** [April 13, 2022, 1:56pm UTC](https://discourse.julialang.org/t/i-keep-getting-this-error-syntax-invalid-iteration-specification/79357/14 "2022-04-13T13:56:18Z")

</div>

This works perfectly now. Thank you very much for the help.  
Highly appreciated
