Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to extract variable names from a list of transformation rules mapping = {var1 -> 'val1', var2 -> 'val2' }. Variables var1 and var2 are already defined in my notebook:

var1 = 'init-val1'; var2 = 'init-val2';

When I try to evaluate Extract[mapping, {1,1}, HoldForm], I get 'init-val1', instead of var1.

What am I doing wrong? Why is var1 getting evaluated despite the explicit HoldForm wrapper? How can I fix the problem?

share|improve this question
Check your mapping... the evaluation has already happened when you assigned it to mapping, so the HoldForm is useless. If you explained the intent behind what you're trying to do, then perhaps we can provide a solution that doesn't involve playing catch up with the evaluator. – R. M. Feb 16 '14 at 21:30
@rm-rf, when I evaluate Definition[mapping] I still get {var1 -> 'val1', var2 -> 'val2' }. So in the definition I don't get the substitution of var1 for 'init-val1'. – verse Feb 16 '14 at 21:32
var1 = "init-val1"; mapping = {var1 -> "val1", var2 -> "val2"}; Definition[mapping] gives mapping = {"init-val1" -> "val1", var2 -> "val2"}as expected – Dr. belisarius Feb 16 '14 at 21:38
@belisarius If you do it the other way round, Definition will give you what the OP said. This is different, because in your case, clearing var1 has no effect on mapping but it does in this one. – R. M. Feb 16 '14 at 22:01
@rm-rf So what does "Variables var1 and var2 are already defined in my notebook:" mean? – Dr. belisarius Feb 16 '14 at 22:07
up vote 2 down vote accepted

This kind of problem is the reason I sought and found a solution to:

Here is an example of how the step function defined in my answer there is used:

var1 = "init-val1"; var2 = "init-val2";

mapping := {var1 -> "val1", var2 -> "val2"}   (* note use of := *)

step[mapping][[{1}, 1, 1]]

% // InputForm


The return is var1 wrapped in HoldForm. This is because step[mapping] returns:

HoldForm[{var1 -> "val1", var2 -> "val2"}]

And the use of {1} in Part preserves the head; see:

Recommended reading:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.