2
$\begingroup$

On Mathematica 12.2 the code

x = <||>;
AssociateTo[x, "foo" -> {{1, 2}, {3, 4}}]
x["foo"][[1]] += {0, 1}

raises the error

Set::setps: x[foo] in the part assignment is not a symbol.

whereas

x["foo"][[1]]

returns {1,2} just fine.

Should the increment work?

$\endgroup$
2
  • $\begingroup$ Closing as this is in the docs. Read ref/message/General/setps in the documentation center. It says Part assignments are implemented only for parts of the value of a symbol. . The x["foo"][[1]] is not a symbol so you cannot set it with =. $\endgroup$
    – flinty
    Commented Sep 13, 2021 at 11:12
  • $\begingroup$ closely related: Part not behaving the way I expect it to $\endgroup$
    – Kuba
    Commented Sep 13, 2021 at 12:07

1 Answer 1

0
$\begingroup$

You could access the subparts of the association via x["foo"][[1]], but you could also access it as x[["foo", 1]]. Knowing that, your increment statement should be written

In[73]:= x[["foo", 1]] += {0, 1}

Out[73]= {1, 3}
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.