Extracting elements from dictionary containing a struct, where the struct is made up of a dictionary containing a nested struct

#Initializing the 2 structs
struct inner
    value::Float64
end

struct outer
    inner::Any
end

#Initializing the 2 dictionaries
outer_dic = Dict()
inner_dic = Dict()

#Defining the values for the inner dictionary
inner_dic[1] = inner(1)
inner_dic[2] = inner(2)
inner_dic[3] = inner(3)

#Defining the values for the outer dictionary
outer_dic[1] = outer([inner_dic[1],inner_dic[2],inner_dic[3]])

#Goal
#This does not work but this is what I would like to do as simple as possible, preferably without for-loops
array = collect(outer_dic[1].inner[1:3].value)
1 Like
@show getfield.((outer_dic[1].inner[i] for i in 1:3), :value)

works for me.

2 Likes
outer_dic[1].inner

?

Thank you! It works like a charm!

That only extracts a vector of the inner structs, and not the actual values inside the inner structs. But I believe that what goerch suggested works :slight_smile:

collect(outer_dic[1].inner[i].value for i in 1:3)

The above also works and is a pretty clean solution I think!

2 Likes
getfiled.(od[1].inner,:value)

?

1 Like