Convert defined variables to rule list












3












$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?










share|improve this question











$endgroup$








  • 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
















3












$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?










share|improve this question











$endgroup$








  • 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














3












3








3





$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?










share|improve this question











$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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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








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










2 Answers
2






active

oldest

votes


















4












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





share|improve this answer











$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 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$
    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





















3












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





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks - any way to keep the variables' values afterwards?
    $endgroup$
    – Chris K
    8 hours 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',
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
});


}
});














draft saved

draft discarded


















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









4












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





share|improve this answer











$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 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$
    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


















4












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





share|improve this answer











$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 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$
    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
















4












4








4





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





share|improve this answer











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






share|improve this answer














share|improve this answer



share|improve this answer








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 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$
    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$
    @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$
    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













3












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





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks - any way to keep the variables' values afterwards?
    $endgroup$
    – Chris K
    8 hours ago
















3












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





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks - any way to keep the variables' values afterwards?
    $endgroup$
    – Chris K
    8 hours ago














3












3








3





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





share|improve this answer









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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • $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


















draft saved

draft discarded




















































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%2f191022%2fconvert-defined-variables-to-rule-list%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

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana