Message Boards Message Boards

1
|
5915 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to construct hierarchical associations?

Posted 10 years ago

I am trying to make use of the new association and dataset capabilities and ran into a problem constructing a hierarchical structure, as in the code below. Do any of you see what i am doing wrong?

(* define an association *)
as = Association[{}]

(* \[LeftAssociation]\[RightAssociation] *)

(* add a key \[Rule] value at the top level *)
AssociateTo[as, "name" -> "myName"]

(* \[LeftAssociation]"name"\[Rule]"myName"\[RightAssociation] *)

(* add another key whose value is an association *)
AssociateTo[as, "summary" -> Association[{}]]

(* \[LeftAssociation]"name"\[Rule]"myName","summary"\[Rule]\
\[LeftAssociation]\[RightAssociation]\[RightAssociation] *)

(* so as[["summary"]] is an association *)
as[["summary"]] // Head

(* Association *)

(* so why can't I add a key\[Rule]value to it? *)
AssociateTo[as[["summary"]], "stuff" -> "someStuff"]

AssociateTo::rvalue: as[[summary]] is not a variable with a value, so its value cannot be changed. >>

AssociateTo[as[["summary"]], "stuff" -> "someStuff"]
Attachments:
POSTED BY: David Keith
2 Replies
Posted 10 years ago

I have found that while AssociateTo will not work, AppendTo will.

Halyomorpha halys ?

In[6]:= (* This does work *)
AppendTo[as[["summary"]], "stuff" -> "someStuff"]

Out[6]= <|"stuff" -> "someStuff"|>

In[7]:= as

Out[7]= <|"name" -> "myName", "summary" -> <|"stuff" -> "someStuff"|>|
 >
POSTED BY: David Keith
Posted 10 years ago
Attachments:
POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract