Message Boards Message Boards

2 Replies
1 Total Likes
View groups...
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"]
POSTED BY: David Keith
2 Replies
Posted 10 years ago

I submitted this to Tech support and received the reply that the behavior is caused by the fact that AssociateTo has attribute HoldFirst, so the expression is sees is not an association. The reply recommended Append as a work-around. (Which I had already found.)

However, if HoldFirst is the problem, then I would think that Evaluate would clear it: "Evaluate[expr] causes expr to be evaluated even if it appears as the argument of a function whose attributes specify that it should be held unevaluated." However, it has no effect, as seen in the attached associations-evaluate.nb.

I have been continuing to try to make use of the new Dataset functionality, which is supposed to "support an unprecedentedly flexible and general approach to structured datasets." It has been very frustrating. Attached is another notebook (associations3.nb), in which I try to construct a structured dataset, and then access it through its keys. Nothing seems to work. I can ask for a list of the keys, see that they are there, but when I try to use one, I get key missing. It is very possible, even probable, that my syntax is incorrect. But I have been through the documentation over and over -- it doesn't help that the descriptions of dataset functionality is distributed across the various overloaded operators.

If anyone here can offer insight I would really appreciate it.

And Bruce, Sean, Daniel: In this forum, questions on Associations and Datasets generally go unanswered. Is it possible you might encourage one of your colleagues from that part of the effort to put in an appearance?

POSTED BY: David Keith
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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract