# Iterate over Fields of named tupel

**URL:** https://discourse.julialang.org/t/iterate-over-fields-of-named-tupel/60294
**Category:** General Usage
**Tags:** question
**Created:** [April 30, 2021, 9:16am UTC](https://discourse.julialang.org/t/iterate-over-fields-of-named-tupel/60294 "2021-04-30T09:16:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nonameclimber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nonameclimber/32/24555_2.png) [@nonameclimber](https://discourse.julialang.org/u/nonameclimber)
#### Post date: [April 30, 2021, 9:16am UTC](https://discourse.julialang.org/t/iterate-over-fields-of-named-tupel/60294/1 "2021-04-30T09:16:13Z")

</div>

Hi,  
I have a named tupel with 3 fields with Int64-Array and 1 field with a `struct` called “agent”.  
`::NamedTuple{(:S, :I, :R, :agents),Tuple{Array{Any,1},Array{Any,1},Array{Any,1},Array{Main.workspace3.Agent,1}}}`  
Is there a way to iterate over the first three fields of this tuple?  
Until now I just copy/pasted the code I want to run for the fields:

```julia
mean_Ss = first(simulations).S
for sim in simulations[2:end]
	mean_Ss = hcat(mean_Ss, sim.S)
end
mean_Ss = mean.(eachrow(mean_Ss))
plot!(p, 1:T, mean_Ss, label="mean S agents")

```

My approach was to create a `for`-loop like this:

```julia
for x ∈ [S, I, R]
	mean_status = getproperty(first(simulations), x)
#	or like this: first(simulations).x
	for sim in simulations[2:end]
		mean = hcat(mean, sim.x)
	end
mean = mean.(eachrow(mean))		
end

```

But all approaches result in an error:  
`MethodError: no method matching getproperty(::NamedTuple{(:S, :I, :R, :agents),Tuple{Array{Any,1},Array{Any,1},Array{Any,1},Array{Main.workspace3.Agent,1}}}, ::Main.workspace3.InfectionStatus)`

For a better understanding:

```julia
mutable struct Agent
	status::InfectionStatus
	num_infected::Int64
end

```

```julia
@enum InfectionStatus S I R

```

---

<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 30, 2021, 9:42am UTC](https://discourse.julialang.org/t/iterate-over-fields-of-named-tupel/60294/2 "2021-04-30T09:42:58Z")

</div>

> [@nonameclimber](#):
>
> `for x ∈ [S, I, R]`

I didn’t run this but I think the problem is here. If S I and R weren’t assigned you’d get a more helpful error. The “field names” are of type `Symbol` internally, so you probably want  
`for x ∈ [:S, :I, :R] `

> [@nonameclimber](#):
>
> `hcat(mean, sim.x)`

And at this place, you try to access a field “x” of sym, not the fieldname stored in x. Replace this with `getproperty` as you did above.

Edit, expanded: `thing.name` is just sugar for `getproperty(thing, :name)` and this is static (if it weren’t, you could shadow fieldnames with local variables, which would be super confusing. So if you want to compute a name, you always need the longer `getproperty` version. Good luck!

---

<div class="post-metadata">

### Author: ![nonameclimber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nonameclimber/32/24555_2.png) [@nonameclimber](https://discourse.julialang.org/u/nonameclimber)
#### Post date: [April 30, 2021, 9:51am UTC](https://discourse.julialang.org/t/iterate-over-fields-of-named-tupel/60294/3 "2021-04-30T09:51:05Z")

</div>

> [@FPGro](#):
>
> I didn’t run this but I think the problem is here. If S I and R weren’t assigned you’d get a more helpful error. The “field names” are of type `Symbol` internally, so you probably want  
> `for x ∈ [:S, :I, :R]`

Thanks! That did the magic!  
With:

```julia
for x ∈ [:S, :I, :R]
    mean_status = getproperty(first(simulations), x)
    for sim in simulations[2:end]
        mean = hcat(mean, getproperty(sim, x))
    end
mean = mean.(eachrow(mean))        
end

```

It now runs through!
