# Using structs in for loops?

**URL:** https://discourse.julialang.org/t/using-structs-in-for-loops/25998
**Category:** New to Julia
**Created:** [July 3, 2019, 10:47pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998 "2019-07-03T22:47:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [July 3, 2019, 10:47pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/1 "2019-07-03T22:47:59Z")

</div>

Hey guys

Can anyone explain to me why k is not defined in this case?

```julia
struct MyStruct
    id::Int
     x::Float64
     y::Float64
end

function SquareGrid(nStart,nEnd,nStep)
    x = collect(range(nStart,nEnd, step=nStep))
    nP = size(x)[1]
    for i = 1:nP
        k = MyStruct(i,x[i],x[i])
        println(k)
    end
    return k
end

```

It gives me the error:

```julia
ERROR: UndefVarError: k not defined
Stacktrace:
 [1] SquareGrid(::Int64, ::Int64, ::Float64) at C:\Users\Ahmed Salih\OneDrive - Aarhus universitet\AU-CIVIL1\2\ThermoSim\Grid.jl:32
 [2] top-level scope at none:0

```

Did they change something with scope again? Using Julia 1.1.1

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [July 3, 2019, 10:54pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/2 "2019-07-03T22:54:28Z")

</div>

Figured it out, I just had to write “global” infront of k in the for loop. Maybe there is a better way? My approach is not working so well right now, since I want “k” ie. my struct to hold x and y location for many nodes of different id’s - just can’t figure out how to achieve that

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 3, 2019, 11:03pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/3 "2019-07-03T23:03:39Z")

</div>

You can write `local k` in front of the loop.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [July 3, 2019, 11:29pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/4 "2019-07-03T23:29:03Z")

</div>

> [@kristoffer.carlsson](#):
>
> locak

Thanks! That worked

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [July 4, 2019, 2:24am UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/5 "2019-07-04T02:24:22Z")

</div>

> [@Ahmed\_Salih](#):
>
> I just had to write “global” infront of k in the for loop.

You want to avoid `global` like the plague… Unless it is a very deliberate design decision, which is rarely necessary outside of packages

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [July 4, 2019, 1:51pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/6 "2019-07-04T13:51:22Z")

</div>

I am trying to generate a grid of nodes (ie. a grid of points) and I want each point to be described by the “MyStruct”, so that in the future I can access the values of this node using MyStruct (via the id field name) - I don’t know if this is the best approach but what I am doing currently is not working since MyStruct is only able to handle one point at a time… Any suggestions?

Kind regards

---

<div class="post-metadata">

### Author: ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)
#### Post date: [July 4, 2019, 4:04pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/7 "2019-07-04T16:04:13Z")

</div>

My suggestion is to avoid defining custom structures until you get a little more comfortable with the language. Unless you the structures for multiple dispatch, having it return matrices of the `id,x,y` (which you can collect in a named tuple if you wish to have them in one place) may be easier.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 4, 2019, 4:31pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/8 "2019-07-04T16:31:13Z")

</div>

> [@jlperla](#):
>
> My suggestion is to avoid defining custom structures until you get a little more comfortable with the language.

Using structs is pretty much the first thing you learn about the language after some basic syntax.

> [@Ahmed\_Salih](#):
>
> I don’t know if this is the best approach but what I am doing currently is not working since MyStruct is only able to handle one point at a time… Any suggestions?

One suggestion would be to have an array of `MyStruct` so something like

```julia
struct MyStruct
    id::Int
     x::Float64
     y::Float64
end

function SquareGrid(nStart,nEnd,nStep)
    x = collect(range(nStart,nEnd, step=nStep))
    nP = size(x)[1]
    return [MyStruct(i, x[i], x[i]) for i in 1:nP]
end

```

and then you get

```julia
julia> ks = SquareGrid(1, 10, 1)
10-element Array{MyStruct,1}:
 MyStruct(1, 1.0, 1.0)
 MyStruct(2, 2.0, 2.0)
 MyStruct(3, 3.0, 3.0)
 MyStruct(4, 4.0, 4.0)
 MyStruct(5, 5.0, 5.0)
 MyStruct(6, 6.0, 6.0)
 MyStruct(7, 7.0, 7.0)
 MyStruct(8, 8.0, 8.0)
 MyStruct(9, 9.0, 9.0)
 MyStruct(10, 10.0, 10.0)

julia> ks[3].id
3

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [July 4, 2019, 9:48pm UTC](https://discourse.julialang.org/t/using-structs-in-for-loops/25998/9 "2019-07-04T21:48:03Z")

</div>

Thanks exactly what I wanted, and thank you for showcasing how to access the individual elements. If someone wants to extract the whole array I discovered this notation:

```julia
ks.↦ :id

```

Taken from this answer [Best way to access field in array of structs in Julia - Stack Overflow](https://stackoverflow.com/questions/46820304/best-way-to-access-field-in-array-of-structs-in-julia)
