Convert defined variables to rule list
$begingroup$
I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g.
x = 1;
y = 2;
VariablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> z} *)
Is this even possible?
function-construction evaluation replacement
$endgroup$
add a comment |
$begingroup$
I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g.
x = 1;
y = 2;
VariablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> z} *)
Is this even possible?
function-construction evaluation replacement
$endgroup$
1
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order toClear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.
$endgroup$
– march
9 hours ago
$begingroup$
IsOwnValues /@ Unevaluated@{x, y, z}
OK?
$endgroup$
– xzczd
2 hours ago
add a comment |
$begingroup$
I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g.
x = 1;
y = 2;
VariablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> z} *)
Is this even possible?
function-construction evaluation replacement
$endgroup$
I'd like to make a function that takes a list of variables and returns a corresponding rule list with the current values of the variables. E.g.
x = 1;
y = 2;
VariablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> z} *)
Is this even possible?
function-construction evaluation replacement
function-construction evaluation replacement
edited 8 hours ago
march
17.2k22769
17.2k22769
asked 9 hours ago
Chris KChris K
6,58421841
6,58421841
1
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order toClear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.
$endgroup$
– march
9 hours ago
$begingroup$
IsOwnValues /@ Unevaluated@{x, y, z}
OK?
$endgroup$
– xzczd
2 hours ago
add a comment |
1
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order toClear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.
$endgroup$
– march
9 hours ago
$begingroup$
IsOwnValues /@ Unevaluated@{x, y, z}
OK?
$endgroup$
– xzczd
2 hours ago
1
1
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order to
Clear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.$endgroup$
– march
9 hours ago
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order to
Clear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.$endgroup$
– march
9 hours ago
$begingroup$
Is
OwnValues /@ Unevaluated@{x, y, z}
OK?$endgroup$
– xzczd
2 hours ago
$begingroup$
Is
OwnValues /@ Unevaluated@{x, y, z}
OK?$endgroup$
– xzczd
2 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Update 2
Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var
and according to my original interpretation of the problem:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]
Update 1
After some comments from the OP, it seems they want instead something like
variableToRule[var_] := SymbolName@Unevaluated@var -> var
instead.
Original Post
Here's a first iteration. First define the helper function,
ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
, Clear@var
; var -> val
]
Then, the function is
ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars
This uses the trick from this answer.
Then,
x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values ofx
andy
, etc. in expressions that contain those variables? In that place, you don't wantx
andy
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get{1 -> 1, 2 -> 2}
.
$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK
$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
|
show 6 more comments
$begingroup$
Pass in the names of the symbols as strings, and the rest is quite easy:
ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
s // Apply[ClearAll];
MapThread[Rule, {Symbol /@ s, t}]
];
ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
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
});
}
});
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%2f191022%2fconvert-defined-variables-to-rule-list%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$
Update 2
Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var
and according to my original interpretation of the problem:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]
Update 1
After some comments from the OP, it seems they want instead something like
variableToRule[var_] := SymbolName@Unevaluated@var -> var
instead.
Original Post
Here's a first iteration. First define the helper function,
ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
, Clear@var
; var -> val
]
Then, the function is
ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars
This uses the trick from this answer.
Then,
x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values ofx
andy
, etc. in expressions that contain those variables? In that place, you don't wantx
andy
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get{1 -> 1, 2 -> 2}
.
$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK
$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
|
show 6 more comments
$begingroup$
Update 2
Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var
and according to my original interpretation of the problem:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]
Update 1
After some comments from the OP, it seems they want instead something like
variableToRule[var_] := SymbolName@Unevaluated@var -> var
instead.
Original Post
Here's a first iteration. First define the helper function,
ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
, Clear@var
; var -> val
]
Then, the function is
ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars
This uses the trick from this answer.
Then,
x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values ofx
andy
, etc. in expressions that contain those variables? In that place, you don't wantx
andy
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get{1 -> 1, 2 -> 2}
.
$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK
$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
|
show 6 more comments
$begingroup$
Update 2
Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var
and according to my original interpretation of the problem:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]
Update 1
After some comments from the OP, it seems they want instead something like
variableToRule[var_] := SymbolName@Unevaluated@var -> var
instead.
Original Post
Here's a first iteration. First define the helper function,
ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
, Clear@var
; var -> val
]
Then, the function is
ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars
This uses the trick from this answer.
Then,
x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
Update 2
Based on a suggestion by Somos, the following version is nicer. According to what the OP wants:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := SymbolName@Unevaluated@var -> var
and according to my original interpretation of the problem:
SetAttributes[variableToRule, {HoldAll, Listable}]
variableToRule[var_] := Module[{val = var}, Clear@var; var -> val]
Update 1
After some comments from the OP, it seems they want instead something like
variableToRule[var_] := SymbolName@Unevaluated@var -> var
instead.
Original Post
Here's a first iteration. First define the helper function,
ClearAll@variableToRule
SetAttributes[variableToRule, HoldAll]
variableToRule[var_] := Module[{val = var}
, Clear@var
; var -> val
]
Then, the function is
ClearAll@variablesToRules
SetAttributes[variablesToRules, HoldAll]
variablesToRules[vars_List] := variableToRule /@ Unevaluated@vars
This uses the trick from this answer.
Then,
x = 1; y = 2; z = 3;
variablesToRules[{x, y, z}]
(* {x -> 1, y -> 2, z -> 3} *)
edited 6 hours ago
answered 9 hours ago
marchmarch
17.2k22769
17.2k22769
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values ofx
andy
, etc. in expressions that contain those variables? In that place, you don't wantx
andy
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get{1 -> 1, 2 -> 2}
.
$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK
$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
|
show 6 more comments
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values ofx
andy
, etc. in expressions that contain those variables? In that place, you don't wantx
andy
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get{1 -> 1, 2 -> 2}
.
$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK
$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values of
x
and y
, etc. in expressions that contain those variables? In that place, you don't want x
and y
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get {1 -> 1, 2 -> 2}
.$endgroup$
– march
8 hours ago
$begingroup$
@ChrisK. Your question confuses me. If you're making replacement rules, isn't the whole point that you will use them to replace the values of
x
and y
, etc. in expressions that contain those variables? In that place, you don't want x
and y
set before-hand. What is it that you are trying to do here? Is this just for display purposes or something? If you keep the variables set, then you will get {1 -> 1, 2 -> 2}
.$endgroup$
– march
8 hours ago
$begingroup$
I think the trick from your link works:
variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK$endgroup$
– Chris K
8 hours ago
$begingroup$
I think the trick from your link works:
variableToRule[var_] := SymbolName[Unevaluated@var] -> var
seems OK$endgroup$
– Chris K
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
But then, what is the point of the replacement rule? Because that won't actually work as a replacement rule, because you have the symbol name (which is a string), instead of the symbol itself.
$endgroup$
– march
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
$begingroup$
It's a bit hard to explain, but I've got an inner function that takes a list of rules as an argument, which needs to be defined in an outer function where the variables are already defined. Anyhow I think I'm sorted now. Thanks!
$endgroup$
– Chris K
8 hours ago
|
show 6 more comments
$begingroup$
Pass in the names of the symbols as strings, and the rest is quite easy:
ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
s // Apply[ClearAll];
MapThread[Rule, {Symbol /@ s, t}]
];
ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
add a comment |
$begingroup$
Pass in the names of the symbols as strings, and the rest is quite easy:
ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
s // Apply[ClearAll];
MapThread[Rule, {Symbol /@ s, t}]
];
ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
add a comment |
$begingroup$
Pass in the names of the symbols as strings, and the rest is quite easy:
ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
s // Apply[ClearAll];
MapThread[Rule, {Symbol /@ s, t}]
];
ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
$endgroup$
Pass in the names of the symbols as strings, and the rest is quite easy:
ClearAll[varsToRules];
varsToRules[s_] := With[{t = Map[Symbol, s]},
s // Apply[ClearAll];
MapThread[Rule, {Symbol /@ s, t}]
];
ClearAll[x, y];
{x, y, z} = {1, 2, 3};
varsToRules[{"x", "y", "z"}]
(* {x -> 1, y -> 2, z -> 3} *)
answered 8 hours ago
ShredderroyShredderroy
1,5701115
1,5701115
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
add a comment |
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
$begingroup$
Thanks - any way to keep the variables' values afterwards?
$endgroup$
– Chris K
8 hours ago
add a comment |
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%2f191022%2fconvert-defined-variables-to-rule-list%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
1
$begingroup$
This is a super interesting question. This post is related, because you will need to get the symbol names in order to
Clear
them, but it's not clear how to short-circuit the evaluation when you will be feeding a list of variable names to the function rather than just the variable name.$endgroup$
– march
9 hours ago
$begingroup$
Is
OwnValues /@ Unevaluated@{x, y, z}
OK?$endgroup$
– xzczd
2 hours ago