Is it possible to set values for a list of variables using a for loop?Assign one button to two...

Are all power cords made equal?

Why did Ylvis use "go" instead of "say" in phrases like "Dog goes 'woof'"?

Why is Shelob considered evil?

Is the UK legally prevented from having another referendum on Brexit?

Is there a celebrity culture in academia and should we discourage it?

How can I put a period right after the algorithm's number in the algorithm's title?

What happens to the first ionization potential when a hydrogen-like atom captures a particle?

Players preemptively rolling, even though their rolls are useless or are checking the wrong skills

Is practicing on a digital piano harmful to an experienced piano player?

Renting a 2CV in France

How can I differentiate duration vs starting time

What is an efficient way to digitize a family photo collection?

How can I keep my gold safe from other PCs?

Can I use a single resistor for multiple LED with different +ve sources?

Can you say "leftside right"?

Dealing with an internal ScriptKiddie

How bad is a Computer Science course that doesn't teach Design Patterns?

Running away from a conflict

Is the percentage symbol a constant?

Multiple null checks in Java 8

In the Lost in Space intro why was Dr. Smith actor listed as a special guest star?

What if I miss a connection and don't have money to book next flight?

Is Screenshot Time-tracking Common?

Did ancient Germans take pride in leaving the land untouched?



Is it possible to set values for a list of variables using a for loop?


Assign one button to two variablesAssign variable's value programmaticallyUnderstanding Function and Variable in Mathematica for beginnerReplacing multiple instances of a symbol with different variables and assignmentsAppend fixed value to random selection of listUsing delayed evaluation on module variables called through an associationHow can I assign many values each to a different variable?How to return the unevaluated variable names from a list of variables which have been declaredAssignment via iteration over list of variables to be assigned valuesWriting a variable to file with its value, not its name













1












$begingroup$


Let me preface this question by noting that this is a simple example meant to clarify what I am asking. The actual context in which I want to implement this involves associations containing hundreds of variables, nested for loops, and writing to/reading from files. Because of this, the traditional method of setting a list of variables to a list of values (e.g. {a,b,c}={1,2,3}) is not practical.



Here is my simplified example:



Suppose I have a list of variables which have already been defined: a,b,c,d,e,f. I want to set these variables to values of 1,2,3,4,5,6 using a for loop.



To do this, I define the list:



listOfVar = {"a","b","c","d","e","f"}


And naively use the following for loop



For[i=1,i<=listOfVar,i++,
listOfVar[[i]] = i
]


Clearly, this won't work, as it will just set



listOfVar = {1,2,3,4,5}


My question is thus:



How do I replace listOfVar[[i]] with the variable name which its string represents? (e.g. how do I replace listOfVar[[3]] with the variable c so that the for loop correctly sets c to the value of 3?)



I have tried many combinations of Hold[],ToExpression[],ToString[],etc., but I do not seem to have enough knowledge of the low-level operations of Mathematica to solve this problem (or perhaps I am overlooking an obvious solution for whatever reason). I would greatly appreciate the help, as this is a problem I have given up on multiple times.










share|improve this question









New contributor




user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    1












    $begingroup$


    Let me preface this question by noting that this is a simple example meant to clarify what I am asking. The actual context in which I want to implement this involves associations containing hundreds of variables, nested for loops, and writing to/reading from files. Because of this, the traditional method of setting a list of variables to a list of values (e.g. {a,b,c}={1,2,3}) is not practical.



    Here is my simplified example:



    Suppose I have a list of variables which have already been defined: a,b,c,d,e,f. I want to set these variables to values of 1,2,3,4,5,6 using a for loop.



    To do this, I define the list:



    listOfVar = {"a","b","c","d","e","f"}


    And naively use the following for loop



    For[i=1,i<=listOfVar,i++,
    listOfVar[[i]] = i
    ]


    Clearly, this won't work, as it will just set



    listOfVar = {1,2,3,4,5}


    My question is thus:



    How do I replace listOfVar[[i]] with the variable name which its string represents? (e.g. how do I replace listOfVar[[3]] with the variable c so that the for loop correctly sets c to the value of 3?)



    I have tried many combinations of Hold[],ToExpression[],ToString[],etc., but I do not seem to have enough knowledge of the low-level operations of Mathematica to solve this problem (or perhaps I am overlooking an obvious solution for whatever reason). I would greatly appreciate the help, as this is a problem I have given up on multiple times.










    share|improve this question









    New contributor




    user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      1












      1








      1





      $begingroup$


      Let me preface this question by noting that this is a simple example meant to clarify what I am asking. The actual context in which I want to implement this involves associations containing hundreds of variables, nested for loops, and writing to/reading from files. Because of this, the traditional method of setting a list of variables to a list of values (e.g. {a,b,c}={1,2,3}) is not practical.



      Here is my simplified example:



      Suppose I have a list of variables which have already been defined: a,b,c,d,e,f. I want to set these variables to values of 1,2,3,4,5,6 using a for loop.



      To do this, I define the list:



      listOfVar = {"a","b","c","d","e","f"}


      And naively use the following for loop



      For[i=1,i<=listOfVar,i++,
      listOfVar[[i]] = i
      ]


      Clearly, this won't work, as it will just set



      listOfVar = {1,2,3,4,5}


      My question is thus:



      How do I replace listOfVar[[i]] with the variable name which its string represents? (e.g. how do I replace listOfVar[[3]] with the variable c so that the for loop correctly sets c to the value of 3?)



      I have tried many combinations of Hold[],ToExpression[],ToString[],etc., but I do not seem to have enough knowledge of the low-level operations of Mathematica to solve this problem (or perhaps I am overlooking an obvious solution for whatever reason). I would greatly appreciate the help, as this is a problem I have given up on multiple times.










      share|improve this question









      New contributor




      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      Let me preface this question by noting that this is a simple example meant to clarify what I am asking. The actual context in which I want to implement this involves associations containing hundreds of variables, nested for loops, and writing to/reading from files. Because of this, the traditional method of setting a list of variables to a list of values (e.g. {a,b,c}={1,2,3}) is not practical.



      Here is my simplified example:



      Suppose I have a list of variables which have already been defined: a,b,c,d,e,f. I want to set these variables to values of 1,2,3,4,5,6 using a for loop.



      To do this, I define the list:



      listOfVar = {"a","b","c","d","e","f"}


      And naively use the following for loop



      For[i=1,i<=listOfVar,i++,
      listOfVar[[i]] = i
      ]


      Clearly, this won't work, as it will just set



      listOfVar = {1,2,3,4,5}


      My question is thus:



      How do I replace listOfVar[[i]] with the variable name which its string represents? (e.g. how do I replace listOfVar[[3]] with the variable c so that the for loop correctly sets c to the value of 3?)



      I have tried many combinations of Hold[],ToExpression[],ToString[],etc., but I do not seem to have enough knowledge of the low-level operations of Mathematica to solve this problem (or perhaps I am overlooking an obvious solution for whatever reason). I would greatly appreciate the help, as this is a problem I have given up on multiple times.







      assignment






      share|improve this question









      New contributor




      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      Henrik Schumacher

      54.6k474153




      54.6k474153






      New contributor




      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      user63017user63017

      405




      405




      New contributor




      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          3












          $begingroup$

          listOfVar = {"a", "b", "c", "d", "e", "f"};
          ClearAll @@ listOfVar;
          Do[
          ToExpression[listOfVar[[i]], InputForm, Set[#, i] &],
          {i, 1, Length[listOfVar]}
          ];
          Symbol /@ listOfVar



          {1, 2, 3, 4, 5, 6}







          share|improve this answer









          $endgroup$





















            1












            $begingroup$

            Here is a second answer, if anyone is curious. It is a modification of an answer to another question I found after searching for an hour or two.



            ClearAll @@ listOfVar;
            For[i = 1, i <= Length[listOfVar], i++,
            Evaluate[Symbol[listOfSavedVariables[[i]]]] = i;
            ]





            share|improve this answer










            New contributor




            user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$













              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              user63017 is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192102%2fis-it-possible-to-set-values-for-a-list-of-variables-using-a-for-loop%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              listOfVar = {"a", "b", "c", "d", "e", "f"};
              ClearAll @@ listOfVar;
              Do[
              ToExpression[listOfVar[[i]], InputForm, Set[#, i] &],
              {i, 1, Length[listOfVar]}
              ];
              Symbol /@ listOfVar



              {1, 2, 3, 4, 5, 6}







              share|improve this answer









              $endgroup$


















                3












                $begingroup$

                listOfVar = {"a", "b", "c", "d", "e", "f"};
                ClearAll @@ listOfVar;
                Do[
                ToExpression[listOfVar[[i]], InputForm, Set[#, i] &],
                {i, 1, Length[listOfVar]}
                ];
                Symbol /@ listOfVar



                {1, 2, 3, 4, 5, 6}







                share|improve this answer









                $endgroup$
















                  3












                  3








                  3





                  $begingroup$

                  listOfVar = {"a", "b", "c", "d", "e", "f"};
                  ClearAll @@ listOfVar;
                  Do[
                  ToExpression[listOfVar[[i]], InputForm, Set[#, i] &],
                  {i, 1, Length[listOfVar]}
                  ];
                  Symbol /@ listOfVar



                  {1, 2, 3, 4, 5, 6}







                  share|improve this answer









                  $endgroup$



                  listOfVar = {"a", "b", "c", "d", "e", "f"};
                  ClearAll @@ listOfVar;
                  Do[
                  ToExpression[listOfVar[[i]], InputForm, Set[#, i] &],
                  {i, 1, Length[listOfVar]}
                  ];
                  Symbol /@ listOfVar



                  {1, 2, 3, 4, 5, 6}








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Henrik SchumacherHenrik Schumacher

                  54.6k474153




                  54.6k474153























                      1












                      $begingroup$

                      Here is a second answer, if anyone is curious. It is a modification of an answer to another question I found after searching for an hour or two.



                      ClearAll @@ listOfVar;
                      For[i = 1, i <= Length[listOfVar], i++,
                      Evaluate[Symbol[listOfSavedVariables[[i]]]] = i;
                      ]





                      share|improve this answer










                      New contributor




                      user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      $endgroup$


















                        1












                        $begingroup$

                        Here is a second answer, if anyone is curious. It is a modification of an answer to another question I found after searching for an hour or two.



                        ClearAll @@ listOfVar;
                        For[i = 1, i <= Length[listOfVar], i++,
                        Evaluate[Symbol[listOfSavedVariables[[i]]]] = i;
                        ]





                        share|improve this answer










                        New contributor




                        user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        $endgroup$
















                          1












                          1








                          1





                          $begingroup$

                          Here is a second answer, if anyone is curious. It is a modification of an answer to another question I found after searching for an hour or two.



                          ClearAll @@ listOfVar;
                          For[i = 1, i <= Length[listOfVar], i++,
                          Evaluate[Symbol[listOfSavedVariables[[i]]]] = i;
                          ]





                          share|improve this answer










                          New contributor




                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          $endgroup$



                          Here is a second answer, if anyone is curious. It is a modification of an answer to another question I found after searching for an hour or two.



                          ClearAll @@ listOfVar;
                          For[i = 1, i <= Length[listOfVar], i++,
                          Evaluate[Symbol[listOfSavedVariables[[i]]]] = i;
                          ]






                          share|improve this answer










                          New contributor




                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer








                          edited 19 mins ago





















                          New contributor




                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 24 mins ago









                          user63017user63017

                          405




                          405




                          New contributor




                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          user63017 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






















                              user63017 is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              user63017 is a new contributor. Be nice, and check out our Code of Conduct.













                              user63017 is a new contributor. Be nice, and check out our Code of Conduct.












                              user63017 is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to Mathematica Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f192102%2fis-it-possible-to-set-values-for-a-list-of-variables-using-a-for-loop%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

                              Lupi Siderali Indice Storia | Organizzazione | La Tredicesima Compagnia | Aspetto | Membri Importanti...

                              Armoriale delle famiglie italiane (Car) Indice Armi | Bibliografia | Menu di navigazioneBlasone...