How to make a defined symbol stay in symbol form?

w = 3; g = 4;

{w, g}[[2]]


I want the output to be g and not 3. For example, if I want to save different definitions by DumpSave in different files like below:

Table[DumpSave["/Users/simonlausen/Desktop/Input/ex"<>ToString[i]<>".mx", {w,g}[[i]]],{i,1,2}]

Any suggestions?

  • $\begingroup$ Do you want to clear the numerical value? Clear[g] would do this. $\endgroup$
    – bill s
    Commented Mar 27, 2013 at 17:46
  • $\begingroup$ You can probably also put Block[{w,g}, *** ] around the expression above, to temporarily have no values for w and g $\endgroup$ Commented Mar 27, 2013 at 17:49
  • $\begingroup$ You might want to restrain yourself giving ownvalues to such symbols. Use them rather in local replacements like: Clear[w, g]; (w + g)/. {w->1, g->2}. So if you want the unevaluated symbol form, you can always write {w, g}[[2]] to get g. $\endgroup$ Commented Mar 27, 2013 at 17:57
  • $\begingroup$ I was hoping the question was about conveniently getting a symbol that has a value, without evaluating it. One may think of using Symbol there, which is not convient, or ToExpression["Hold[symb]"], which is ugly, but ToExpression["symb", InputForm, Hold] seems reasonable. $\endgroup$ Commented Mar 27, 2013 at 18:01
  • $\begingroup$ @JacobAkkerboom: that exactly what my question was about. But I haven't found a solution to my second question: How to use a function like DumpSave without manually typing the name of symbol: DumpSave["dir/file.mx",data1] - works fine, but DumpSave["dir/file.mx",{data1,data2,data3}[[1]]] does not. $\endgroup$
    – Simon
    Commented Mar 27, 2013 at 22:26

3 Answers 3


You have to sacrifice something, but it depends on your preferences what you want to keep and what you want to give up. Let's assume you don't want to sacrifice being able to use assignments such as w=3, then you may have to give up using {...} as a wrapper grouping the names of the variables together. You could then define a new wrapper myList to be used instead of {...}:

w = 3; g = 4;

myList[x__] := {ReleaseHold[HoldForm /@ Hold[x]]}

SetAttributes[myList, HoldAll]

myList[w, g][[2]]


This is done by wrapping all elements of myList in HoldForm. You can retrieve the values of these held expressions by applying ReleaseHold to them.


To address the question about DumpSave, we have to modify the strategy a little:

myList[x__] := {ReleaseHold[Unevaluated /@ Hold[x]]}

 DumpSave["ex" <> ToString[i] <> ".mx", #] &[myList[w, g][[i]]], {i, 
  1, 2}]

(* ==> {{3}, {4}} *)

Here, I replaced HoldForm by Unevaluated in myList, so that the symbols g and w are now wrapped by it and will also print with that wrapper. The list of DumpSave commands in the Table then can take these unevaluated arguments as input. However, since DumpSave itself has attribute HoldRest, it wouldn't evaluate these wrappers either. So instead I feed them into DumpSave using the anonymous function ... #]& [..] which doesn't obey the HoldRest attribute.

  • $\begingroup$ Thanks for your answer, but it doesn't work, when I'm trying to use e.g. DumpSave. Actually I was trying to make a function that would convert a directory of csv files into mx files. $\endgroup$
    – Simon
    Commented Mar 28, 2013 at 7:00
  • $\begingroup$ I didn't get what your goal was initially, but I think now the DumpSave should work. $\endgroup$
    – Jens
    Commented Mar 28, 2013 at 7:29
  • $\begingroup$ I tried to run the function but get this: DumpSave::bsnosym: 3 is not defined as a symbol or a context. >> DumpSave::bsnosym: 4 is not defined as a symbol or a context. >> {DumpSave["ex1.mx", Unevaluated[3]], DumpSave["ex2.mx", Unevaluated[4]]} $\endgroup$
    – Simon
    Commented Mar 28, 2013 at 11:40
  • $\begingroup$ You probably forgot to copy the line SetAttributes[myList, HoldAll] $\endgroup$
    – Jens
    Commented Mar 28, 2013 at 15:58
  • $\begingroup$ That's right... Sorry. And thank you again. I think, that I'm beginning to understand it now... $\endgroup$
    – Simon
    Commented Mar 30, 2013 at 21:53

I suppose this answer does not have much added value over that of Jens, but I'll post it anyway. As a remark about the part of the question about DumpSave, an alternative method to that of Jens is the following. I find that in cases where things get evaluated that you don't want to get evaluated, it helps to temporarily replace the function that you want to "do in the end", in this case DumpSave, by Hold. Then in the end you can replace the hold by your function (DumpSave) by using apply (@@). In this case you would proceed as follows

myList2 = {Hold[w], Hold[g]};

 DumpSave @@
     "/Users/simonlausen/Desktop/Input/ex" <> ToString[iii] <> ".mx", 
   {2, 0}
 {iii, 1, 2}
  • $\begingroup$ Thanks for your answer Jacob. I can see, that there are many ways to do the job... $\endgroup$
    – Simon
    Commented Mar 30, 2013 at 21:55
  • $\begingroup$ @SimonLausen No problem. I guess i learned something new too :) $\endgroup$ Commented Mar 31, 2013 at 12:02

I would do this:

w = 3; g = 4;

HoldForm[w, g][[{2}]]
 g   (* wrapped in HoldForm *)

The {} brackets are critical. See this answer for an explanation.

I just read Jens' answer and now realize I missed an important part of the question. I would do something similar to what he did, but I would write it a bit differently:

 DumpSave["path" <> ToString[i] <> ".mx", #] & @ {Unevaluated[w, g]}[[1, {i}]],
 {i, 1, 2}

Ignore the syntax highlighting in Unevaluated[w, g].


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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