I am trying to initialize and manipulate Dicts of Dicts, starting with this one:
using Dates
clcdates=Date(2022,7):Month(1):Date(2023,12)
dacc=Dict("MatA"=>Dict(Date(2022,7)=>751.19,Date(2023,1)=>7.98),
"MatB"=>Dict(Date(2022,10)=>94.25));
So far, so good. Now I want to initialize another Dict of the same type and with the same keys, called acc that is supposed to record the temporal evolution of MatA and MatB in the timespan given by clcdates. Obviously, acc is much more densely populated than dacc, which holds only the changes that occur at irregular dates. I tried:
acc=begin
for dak in keys(dacc)
Dict(dak=>Dict(clcdates[1]=>0))
for m in clcdates[2:end]
Dict(dak=>Dict(clcdates[1]=>NaN))
end
end
end => Dict{String,Dict{Date,Float64}}()
mo=clcdates[1]
for (im,m) in enumerate(clcdates)
for dak in keys(dacc)
if haskey(dacc[dak],m)
acc[dak][m]=acc[dak][mo]+dacc[dak][m] #
elseif im > 1
if acc[dak][mo] == 0
acc[dak][m]=0
acc[dak][mo]=NaN
else
acc[dak][m]=acc[dak][mo]
end
end
end
mo=m
end
This doesn’t work. @show tells me that after the begin...end block, I have acc = nothing => Dict{String, Dict{Date, Float64}}(), and then the first pass of the loop fails with:
MethodError: no method matching getindex(::Pair{Nothing, Dict{String, Dict{Date, Float64}}}, ::String)
Closest candidates are:
getindex(::Pair, ::Int64)
@ Base pair.jl:51
getindex(::Pair, ::Real)
@ Base pair.jl:52
Stacktrace:
[1] top-level scope
@ ./In[34]:18
Line 18 is the line I have marked with # at the end. What’s wrong here? Why is acc nothing, and what does the error message mean? Is the entire approach misguided?