# Accessing an element of a structure based off of a string

**URL:** <https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952>\
**Category:** New to Julia\
**Created:** [August 14, 2020, 7:18pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952 "2020-08-14T19:18:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [August 14, 2020, 7:18pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/1 "2020-08-14T19:18:53Z")

</div>

I want to be able to access and change an element of a structure based off a string that has been read in from a text file and then change the value of that element. The element that is being changed could of several different data types. A simplified version of the structure I am working with is shown below:

```julia
mutable struct SMALLSTRUCTURE
    POPSIZE::Int64
    POPSIZEMAX::Int64
    SEEDMIN::Int64
    SEEDMAX::Int64
    EXPONENT::Float64
    SIGMAINITIAL::Float64
    SIGMAFINAL::Float64
end

mutable struct BIGSTRUCTURE
    SZEGLOBAL::Int64                       
    NUMVAR::Int64
    MAXITER::Int64
    MINVAL::Float64
    MAXVAL::Float64
    BESTPOINT::Array{Float64,1}
    GLOBALPOP::Array{Float64,2}
    GLOBALCOST::Array{Float64,2}
    IWO::SMALLSTRUCTURE
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 14, 2020, 7:23pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/2 "2020-08-14T19:23:02Z")

</div>

```julia
julia> mutable struct SMALLSTRUCTURE
           POPSIZE::Int64
           POPSIZEMAX::Int64
           SEEDMIN::Int64
           SEEDMAX::Int64
           EXPONENT::Float64
           SIGMAINITIAL::Float64
           SIGMAFINAL::Float64
       end

julia> t = SMALLSTRUCTURE(fill(1, 7)...);

julia> setproperty!(t, Symbol("SEEDMIN"), 100)
100

julia> t
SMALLSTRUCTURE(1, 1, 100, 1, 1.0, 1.0, 1.0)

```

You want to use `setproperty!`. Note that you have to use `Symbol`s with `setproperty!`, so I convert the string to a `Symbol`.

---

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [August 14, 2020, 7:41pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/3 "2020-08-14T19:41:43Z")

</div>

My issue with this though is that the string would be “SMALLSTRUCTURE.SEEDMIN” and setproperty!(Symbol(SMALLSTRUCTURE),Symbol(SEEDMIN),5) does not work so how can I also read in the name of the strucutre based off of the input string.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 14, 2020, 7:45pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/4 "2020-08-14T19:45:08Z")

</div>

no, `SMALLSTRUCTURE` is the name of the type. You need an instance of that type, the `t` in that example above.

You should store the variables in that you need to change in a `Dict` which you can access with a `String` key, rather than trying to use a string to look up variables in the global scope

---

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [August 14, 2020, 7:54pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/5 "2020-08-14T19:54:26Z")

</div>

Wait so are you saying that I should not use a structure to store all of my data  
and just use a Dict?

---

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [August 14, 2020, 7:56pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/6 "2020-08-14T19:56:16Z")

</div>

And would it not be possible to pass in t as the string?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 14, 2020, 7:57pm UTC](https://discourse.julialang.org/t/accessing-an-element-of-a-structure-based-off-of-a-string/44952/7 "2020-08-14T19:57:07Z")

</div>

Is the input saying “If `x` is a `SMALLSTRUCTURE`, then update it’s `SEEDMIN` with `100`”?
