How can I update an association with a function?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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!










share|improve this question

























    up vote
    3
    down vote

    favorite












    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!










    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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!










      share|improve this question













      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!







      functions associations






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      Jmeeks29ig

      34818




      34818




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          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|> *)





          share|improve this answer






















          • Thank you Carl. Corrected the typo.
            – Jack LaVigne
            3 hours ago










          • I am wondering if I can ask about the necessity to rename a built-in function.
            – Î‘λέξανδρος Ζεγγ
            2 hours ago










          • @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
            – chris
            29 mins ago










          • @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
            – Jmeeks29ig
            13 mins ago










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181924%2fhow-can-i-update-an-association-with-a-function%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          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|> *)





          share|improve this answer






















          • Thank you Carl. Corrected the typo.
            – Jack LaVigne
            3 hours ago










          • I am wondering if I can ask about the necessity to rename a built-in function.
            – Î‘λέξανδρος Ζεγγ
            2 hours ago










          • @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
            – chris
            29 mins ago










          • @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
            – Jmeeks29ig
            13 mins ago














          up vote
          4
          down vote



          accepted










          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|> *)





          share|improve this answer






















          • Thank you Carl. Corrected the typo.
            – Jack LaVigne
            3 hours ago










          • I am wondering if I can ask about the necessity to rename a built-in function.
            – Î‘λέξανδρος Ζεγγ
            2 hours ago










          • @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
            – chris
            29 mins ago










          • @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
            – Jmeeks29ig
            13 mins ago












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          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|> *)





          share|improve this answer














          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|> *)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 3 hours ago

























          answered 4 hours ago









          Jack LaVigne

          11.2k21329




          11.2k21329











          • Thank you Carl. Corrected the typo.
            – Jack LaVigne
            3 hours ago










          • I am wondering if I can ask about the necessity to rename a built-in function.
            – Î‘λέξανδρος Ζεγγ
            2 hours ago










          • @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
            – chris
            29 mins ago










          • @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
            – Jmeeks29ig
            13 mins ago
















          • Thank you Carl. Corrected the typo.
            – Jack LaVigne
            3 hours ago










          • I am wondering if I can ask about the necessity to rename a built-in function.
            – Î‘λέξανδρος Ζεγγ
            2 hours ago










          • @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
            – chris
            29 mins ago










          • @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
            – Jmeeks29ig
            13 mins ago















          Thank you Carl. Corrected the typo.
          – Jack LaVigne
          3 hours ago




          Thank you Carl. Corrected the typo.
          – Jack LaVigne
          3 hours ago












          I am wondering if I can ask about the necessity to rename a built-in function.
          – Î‘λέξανδρος Ζεγγ
          2 hours ago




          I am wondering if I can ask about the necessity to rename a built-in function.
          – Î‘λέξανδρος Ζεγγ
          2 hours ago












          @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
          – chris
          29 mins ago




          @ΑλέξανδροςΖεγγ because you want to change its Attributes ?
          – chris
          29 mins ago












          @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
          – Jmeeks29ig
          13 mins ago




          @Jack LaVigne, Thanks, I really appreciate that! It makes a lot more sense to me now
          – Jmeeks29ig
          13 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181924%2fhow-can-i-update-an-association-with-a-function%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery