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.

How to make a defined symbol stay in symbol form?

w = 3; g = 4;

{w, g}[[2]]

3

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

Example:

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

Any Suggestions?

share|improve this question
    
Do you want to clear the numerical value? Clear[g] would do this. –  bill s Mar 27 '13 at 17:46
    
You can probably also put Block[{w,g}, *** ] around the expression above, to temporarily have no values for w and g –  Jacob Akkerboom Mar 27 '13 at 17:49
    
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. –  István Zachar Mar 27 '13 at 17:57
    
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. –  Jacob Akkerboom Mar 27 '13 at 18:01
    
@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. –  Simon Lausen Mar 27 '13 at 22:26
add comment

3 Answers

up vote 6 down vote accepted

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

g

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.

Edit

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

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

Table[
 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.

share|improve this answer
    
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. –  Simon Lausen Mar 28 '13 at 7:00
    
I didn't get what your goal was initially, but I think now the DumpSave should work. –  Jens Mar 28 '13 at 7:29
    
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]]} –  Simon Lausen Mar 28 '13 at 11:40
    
You probably forgot to copy the line SetAttributes[myList, HoldAll] –  Jens Mar 28 '13 at 15:58
    
That's right... Sorry. And thank you again. I think, that I'm beginning to understand it now... –  Simon Lausen Mar 30 '13 at 21:53
add comment

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]};

Table[
 DumpSave @@
  Delete[
   Hold[Evaluate[
     "/Users/simonlausen/Desktop/Input/ex" <> ToString[iii] <> ".mx", 
     myList[[iii]]]]
   ,
   {2, 0}
   ]
 ,
 {iii, 1, 2}
 ]
share|improve this answer
    
Thanks for your answer Jacob. I can see, that there are many ways to do the job... –  Simon Lausen Mar 30 '13 at 21:55
    
@SimonLausen No problem. I guess i learned something new too :) –  Jacob Akkerboom Mar 31 '13 at 12:02
add comment

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:

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

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

share|improve this answer
add comment

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.