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
$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.
assignment
New contributor
$endgroup$
add a comment |
$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.
assignment
New contributor
$endgroup$
add a comment |
$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.
assignment
New contributor
$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
assignment
New contributor
New contributor
edited 1 hour ago
Henrik Schumacher
54.6k474153
54.6k474153
New contributor
asked 2 hours ago
user63017user63017
405
405
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$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}
$endgroup$
add a comment |
$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;
]
New contributor
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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}
$endgroup$
add a comment |
$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}
$endgroup$
add a comment |
$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}
$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}
answered 1 hour ago
Henrik SchumacherHenrik Schumacher
54.6k474153
54.6k474153
add a comment |
add a comment |
$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;
]
New contributor
$endgroup$
add a comment |
$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;
]
New contributor
$endgroup$
add a comment |
$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;
]
New contributor
$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;
]
New contributor
edited 19 mins ago
New contributor
answered 24 mins ago
user63017user63017
405
405
New contributor
New contributor
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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