Hey Everyone!
I have incurred in an issue that I believe is conceptual.
I have a struct that I want to fill from a dictionary and i’m encountering problems on accessing the struct fields. The main issue is that the dictionary is variable so I need the function to be quite flexible.
Here is what I was trying to do:
mydict = Dict{Any,Any}("name"=>"joe","color"=>"blue")
struct MyStruct
name::String
color::String
age::Float64
end
function myfunction(dictionary::Dict)
result = Array{MyStruct,1}(undef,1)
for i in keys(dictionary)
if Symbol(i) in fieldnames(MyStruct)
field_name = Symbol(i)
result[1].field_name = dictionary[i]
else
break
end
end
return result
end
my_result = myfunction(mydict)
basically the function first create a 1 element array of MyStruct, then check whether the keys of the dictionary are present in the struct fields. (Here a simplified version that breaks in case this is not true)
In case they are present the function should write in the struct the value of the dictionary for that key.
I’m getting an UndefRefError: access to undefined reference
I got what is the issue (Julia does not recognize field_name has field of MyStruct) But I haven’t find a solution for this.
Thanks for your help!
Luigi
6 posts - 3 participants