Given a list of keys {"a","b","c"} and a value 3 (associated to the last key) I want to define a function that is equivalent to this manual procedure:
w=<||>;
w["a"]=<||>;
w["a"]["b"]=<||>;
w["a"]["b"]["c"]=3;
w
prints:
<|"a" -> <|"b" -> <|"c" -> 3|>|>|>
From {"a","b","c"}->3, it must create keys/values going deeper in the hierarchy.
The solution I have written so far is:
setKeyValue[assoc_Association,{keys___,keyLast_}->value_]:=
Block[{tmp,var,keysAsList},
keysAsList={keys};
tmp=FoldList[If[KeyExistsQ[#1,#2],#1[#2],<||>]&,assoc,keysAsList];
tmp=Reverse[Partition[Riffle[Prepend[keysAsList,Null],tmp],2]];
tmp=Fold[{First[#2],(var=Last[#2];AssociateTo[var,First[#1]->Last[#1]])}&,{keyLast,value},tmp];
Last[tmp]
];
Example with expected behavior:
v=<||>;
v=setKeyValue[v,{"a","b","c"}->3]
v=setKeyValue[v,{"a","b","e","f"}->6]
prints
<|"a" -> <|"b" -> <|"c" -> 3|>|>|>
<|"a" -> <|"b" -> <|"c" -> 3, "e" -> <|"f" -> 6|>|>|>|>
However this function looks so over-complicated that I am sure it exists a more elegant approach.
Any idea?
AssociateNested[org_Association, path_List, value_] := Merge[{org, Fold[<|#2 -> #|> &, value, Reverse@path]},Association]seems to work too. There is certainly something I miss. May you provide some clarification? – Picaud Vincent Jun 05 '19 at 10:19"c" -> 3gone after the second call? – Kuba Jun 05 '19 at 10:22