Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
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. –  rm -rf 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 –  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. –  rm -rf Feb 16 '14 at 22:01
    
@rm-rf So what does "Variables var1 and var2 are already defined in my notebook:" mean? –  belisarius Feb 16 '14 at 22:07

1 Answer 1

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
var1

HoldForm[var1]

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

 
discard

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.