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:
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
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
.
1 Like
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.
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
Wait so are you saying that I should not use a structure to store all of my data
and just use a Dict?
And would it not be possible to pass in t as the string?
Is the input saying “If x
is a SMALLSTRUCTURE
, then update it’s SEEDMIN
with 100
”?