Skip to main content
deleted 98 characters in body; edited tags; edited title
Source Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263

How to update values of nested association using AssociateTo[]AssociateTo?

Saying we have an association:

In[1]:= asc = <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|> Out[1]= <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|>3|>|>; 

I want to updated values of "A". I have tried doing:

In[2]:=AssociateTo[  AssociateTo[asc asc,   asc["A"][#] -> If[asc["A"][#] === 1, 0 , asc["A"][#]] & /@ {"a",  "b", "c"}]   Out[2] <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>, 1 -> 0, 2 -> 2, 3 -> 3|> 

<|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>, 1 -> 0, 2 -> 2, 3 -> 3|>

But this adds values just adds valuesst to the association instead of adding to "A"

How can I update the values in "A" using AssociateTo?

Thanks!

How to update values of nested association using AssociateTo[]?

Saying we have an association:

In[1]:= asc = <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|> Out[1]= <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|> 

I want to updated values of "A". I have tried doing:

In[2]:= AssociateTo[asc, asc["A"][#] -> If[asc["A"][#] === 1, 0 , asc["A"][#]] & /@ {"a",  "b", "c"}]   Out[2] <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>, 1 -> 0, 2 -> 2, 3 -> 3|> 

But this adds values just to association instead of adding to "A"

How can I update the values in "A" using AssociateTo?

Thanks!

How to update values of nested association using AssociateTo?

Saying we have an association:

asc = <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|>; 

I want to updated values of "A". I have tried doing:

AssociateTo[   asc,   asc["A"][#] -> If[asc["A"][#] === 1, 0 , asc["A"][#]] & /@ {"a", "b", "c"}] 

<|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>, 1 -> 0, 2 -> 2, 3 -> 3|>

But this just adds valuesst to the association instead of adding to "A"

How can I update the values in "A" using AssociateTo?

edited title
Link
SuTron
  • 1.8k
  • 1
  • 11
  • 21

How to updatevaluesupdate values of nested association using AssociateTo[]?

Tweeted twitter.com/StackMma/status/665161480753516544
Source Link
SuTron
  • 1.8k
  • 1
  • 11
  • 21

How to updatevalues of nested association?

Saying we have an association:

In[1]:= asc = <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|> Out[1]= <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>|> 

I want to updated values of "A". I have tried doing:

In[2]:= AssociateTo[asc, asc["A"][#] -> If[asc["A"][#] === 1, 0 , asc["A"][#]] & /@ {"a", "b", "c"}] Out[2] <|"A" -> <|"a" -> 1, "b" -> 2, "c" -> 3|>, 1 -> 0, 2 -> 2, 3 -> 3|> 

But this adds values just to association instead of adding to "A"

How can I update the values in "A" using AssociateTo?

Thanks!