# Accessing and changing struct fields using a dictionary

**URL:** https://discourse.julialang.org/t/accessing-and-changing-struct-fields-using-a-dictionary/77954
**Category:** Internals & Design
**Created:** [March 16, 2022, 8:37am UTC](https://discourse.julialang.org/t/accessing-and-changing-struct-fields-using-a-dictionary/77954 "2022-03-16T08:37:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![imantha](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@imantha](https://discourse.julialang.org/u/imantha)
#### Post date: [March 16, 2022, 8:37am UTC](https://discourse.julialang.org/t/accessing-and-changing-struct-fields-using-a-dictionary/77954/1 "2022-03-16T08:37:13Z")

</div>

Hi, I have a struct where I to change its attributes value using a dictionary.

For example

```julia
using Agents: AbstractAgent
mutable struct Casualty <: AbstractAgent
  id :: Int
  pos :: Tuple{Int,Int,Float64}
  trauma :: Int
  awaiting_rescue :: Bool
end

@doc "Update agent attributes" ->
function update_agent_attributes!(agent::Union{Casualty,Rescuer},attr::Dict{Symbol,Any})
    for (k,v) in attr
        @show k,v 
        agent.k = v
    end 
end

a1 = Casualty(1, (775,775,0.0), 2, false)
attr = Dict(:awaiting_rescue, true)

update_agent_attributes!(a1, attr)

>>>
ERROR: type Casualty has no field k

```

Instead of pass the value of k, it has used k as an attribute and hence I get the no field K error.

Any ideas on how I can change the attrbute by passing values in a `Dict`?

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [March 16, 2022, 8:45am UTC](https://discourse.julialang.org/t/accessing-and-changing-struct-fields-using-a-dictionary/77954/2 "2022-03-16T08:45:57Z")

</div>

```julia
for (k,v) in attr
       setfield!(agent,k,v)
end

```
