# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Append a value in a nested association

Contributed by:
Robert Ferguson

ResourceFunction["NestedAssociate"][ gives | |

ResourceFunction["NestedAssociate"][ gives |

ResourceFunction["NestedAssociate"] associates the key sequence to *val* at arbitrary depth within *assoc*.

The form of the specified keys is as in certain built-in functions such as Lookup, and resource functions NestedKeyDrop and NestedLookup in that the outermost Key in Key[*key*] is stripped. This is different from other built-in functions such as Append and KeyExistsQ where the function interprets the key as it is literally given.

Where a key is a list, it must be given as Key[*list*].

Ordering of the given association is preserved.

Keys and values must be associated using Rule.

The use of RuleDelayed is not supported, nor should it appear anywhere in the given *assoc*, as it may cause undesired behaviour.

Unevaluated values in the given association, including nested associations, may be evaluated.

Append a new value to an association at the top level:

In[1]:= |

Out[1]= |

Associate a new value to an existing key, noting that the order of the association is maintained:

In[2]:= |

Out[2]= |

Associate a new value to an existing key at the second level:

In[3]:= |

Out[3]= |

New keys may also be appended at nested levels:

In[4]:= |

Out[4]= |

Nested associations will be created at depth according to the given key specification:

In[5]:= |

Out[5]= |

Arbitrary keys are supported:

In[6]:= |

Out[6]= |

Starting with an association of contacts, append a new contact as an association at the top level:

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

Update an existing value deep in the nested association:

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

When appending a new key/value pair, NestedAssociate is equivalent to Append, but with different key syntax:

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

NestedAssociate guarantees the order of keys is maintained, whereas Append does not:

In[13]:= |

Out[13]= |

In[14]:= |

Out[14]= |

Values can be retrieved from a nested association using resource function NestedLookup:

In[15]:= |

Out[15]= |

Keys can be dropped from a nested association using resource function NestedKeyDrop:

In[16]:= |

Out[16]= |

Where keys are themselves lists, they must be wrapped in Key:

In[17]:= |

Out[17]= |

Not wrapping a list-key in Key returns a Failure:

In[18]:= |

Out[18]= |

Where keys are themselves keys, they must be given in the form Key[Key[*key*]]:

In[19]:= |

Out[19]= |

NestedAssociate strips off the first Key in each key of the key sequence:

In[20]:= |

Out[20]= |

Some built-in functions support this syntax:

In[21]:= |

Out[21]= |

Others do not:

In[22]:= |

Out[22]= |

Avoid this issue by using only strings as keys, and not wrapping keys in Key:

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

Where RuleDelayed is used in the given association, NestedAssociate may cause undesired evaluation:

In[26]:= |

Out[26]= |

- 2.0.0 – 27 December 2019
- 1.0.0 – 23 October 2019

This work is licensed under a Creative Commons Attribution 4.0 International License