On Tue, 2015-08-11 at 05:24, Ian Butterworth <[email protected]> wrote:
> How can I add a dict with 2 different field types (1D array of float and 
> float) to an array nested in a dict that already exists?
>
>
> The 'allmeas' structure already exists up to this point:
>
> allmeas[measkey]["manreps"][num_manrep] 
>
>
> And I'm trying to add like this:
>
> allmeas[measkey]["manreps"][num_manrep]["autoreps"] = Array{Dict{Any,Any},1}  

`Array{Dict{Any,Any},1}` is a type and not an instance of a type.  You
probably want:

allmeas[measkey]["manreps"][num_manrep]["autoreps"] = Dict{Any,Any}[]

this creates a vector which can hold `Dict{Any,Any}` of length zero.  If
you know the length (==n) of the vector use this:

allmeas[measkey]["manreps"][num_manrep]["autoreps"] = Array(Dict{Any,Any},n)

> num_autorep = 1
> meas_1D_array = [1,2,3,4,5,6]
> meas_single_value = 10
>
> newmeas = {"meas_1D_array"=> meas_1D_array ,"meas_single_value"=> 
> meas_single_value}
>
> allmeas[measkey]["manreps"][num_manrep]["autoreps"][num_autorep] = newmeas

If you knew the length, above works.  However, if you have a zero-length
vector, use push! as (unlike in Matlab) you cannot use indexing to
extend arrays

push!(allmeas[measkey]["manreps"][num_manrep]["autoreps"], newmeas)

which adds this to the end of the vector.  

> The error I get is on the last line:
>
> `setindex!` has no method matching setindex!(::Type{Array{Dict{Any,Any},1}}, 
> ::Dict{Any,Any}, ::Int64)
>
>
> If I look at the types of each side, I get:
>
> typeof(allmeas[measkey]["manreps"][num_manrep]["autoreps"]) is 
> Array{Dict{Any,Any},1}

That is a bit odd, I get:

julia> typeof(Array{Dict{Any,Any},1})
DataType

> typeof(newmeas) is Dict{Any,Any}

Aside: to lighten the burden on your helpers and to ensure speedy
answers, try to make your examples as simple as possible, self
contained, and run-able with copy-paste.  For instance, instead of

allmeas[measkey]["manreps"][num_manrep]["autoreps"]

just use a variable:
autoreps = Dict{Any,Any}[]

Reply via email to