# How can I update an association with a function?

I'm not sure if this is a bug, or if I just am not understanding, properly, how to create a function.

I am able to use a list of rules to update an association like so:

AssociateTo[pools, asocs]


Where pools is:

<|p10->-0.271069,p11->-0.748554,p12->-0.772571,p13->0.801179,p14->0.823549,p15->-0.617603,p16->-0.171479,p17->0.860391,p18->-0.0937823,p19->0.142897|>


And asocs is:

{p12 -> -1.0208, p11 -> -1.09988*10^7, p15 -> -0.0190685,
p19 -> 0.075402, p10 -> -0.0904602, p16 -> -0.147492,
p17 -> 0.353486, p13 -> 869.624}


However, when I try to use this function:

updatePoolwithValues[a_, newVals_] := AssociateTo[a, newVals];


I get the following error message:

<|p10->-0.271069,p11->-0.748554,p12->-0.772571,p13->0.801179,p14->0.823549,p15->-0.617603,p16->-0.171479,p17->0.860391,p18->-0.0937823,p19->0.142897|> is not a variable with a value, so its value cannot be changed.


Is this an error, or is there something I am not understanding?

Thanks!

I am going to use simpler numbers and a shorter set of rules and lists to make it easier to focus on the problem.

pool = <|p10 -> 10, p11 -> 11, p12 -> 12, p13 -> 13, p14 -> 14,
p15 -> 15|>;

assoc = {p12 -> -12, p11 -> -11, p16 -> -16};

AssociateTo[pool, assoc]

(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)


After execution check that the symbol pool has been changed.

pool

(* <|p10 -> 10, p13 -> 13, p14 -> 14, p15 -> 15, p12 -> -12,
p11 -> -11, p16 -> -16|> *)


AssociateTo has the attribute HoldFirst

Attributes[AssociateTo]

(* {HoldFirst, Protected} *)


Now let's define your function. I am going to use a slightly different name to clarify what we are attempting to do:

myAssociateTo[a_, newVals_] := AssociateTo[a, newVals]


If you apply this to pool and assoc you will indeed get an error.

myAssociateTo[pool, assoc]

During evaluation of AssociateTo::rvalue: <|p10->10,p11->11,p12->12,
p13->13,p14->14,p15->15|> is not a variable with a value, so its value
cannot be changed.


Let's give myAssociateTo the same attribute as AssociateTo

SetAttributes[myAssociateTo, HoldFirst]


Now we reset pool and try it.

pool = <|p10 -> 10, p11 -> 11, p12 -> 12, p13 -> 13, p14 -> 14,
p15 -> 15|>;

myAssociateTo[pool, assoc]

(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)


The desired result has been achieved.

Note further that the symbol pool has been equated to the new value.

pool

(* <|p10 -> 10, p11 -> -11, p12 -> -12, p13 -> 13, p14 -> 14,
p15 -> 15, p16 -> -16|> *)

• Thank you Carl. Corrected the typo. Commented Sep 14, 2018 at 23:26
• I am wondering if I can ask about the necessity to rename a built-in function. Commented Sep 15, 2018 at 0:30
• @ΑλέξανδροςΖεγγ because you want to change its Attributes ? Commented Sep 15, 2018 at 2:39
• @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now Commented Sep 15, 2018 at 2:55
• @Αλέξανδρος Ζεγγ, Also, I want to put this function in another larger function, and want to be sure I can understand it when I do that. Commented Sep 15, 2018 at 3:11