How can one convert a Mathematica expression into a string without the expression being evaluated first? I tried using Hold, but it returns Hold[expr], instead of expr. Is there any way to get just the expression?
For example, I have a variable var = 5, and I need to assign the name of var to another variable, so that var2 = "var". How can I do this?
My actual use case is this:
I am trying to synchronize my mathematica variables with certain fields in an external database. Every time the external database is updated, I call on a function that is supposed to update my corresponding Mathematica variables. It takes in a list of mappings like {var1 -> "field1", var2 -> "field2"}, and its role is to access the external database, look up the value in "field1", and assign it to var1, then look up the value in "field2" and assign it to var2. The number of variables in the mapping (and their names) can be arbitrary.
The problem is that I need those var1 and var2 variables to be passed as variable names, not values, so that I can assign them inside the function. I do the assignment using
Do[
i[[1]]->i[[2]]/.remoteData,
{i,mappings}
]
where remoteData = {"field1"->"value of field 1", "field2" -> "value of field 2"}. I need i[[1]] to stand for var1, not for its value. I tried setting the attribute SetAttributes[fn,HoldAll] but that didn't help.
The result that I am looking for is var1 = "value of field 1", var 2 = "value of field 2".
MakeBoxes[var], but how you getvarinto that expression will be the issue. What is your actual use for this? – Mr.Wizard Feb 09 '14 at 16:42"var"so presumably you are working with some list of Symbol names or the like that you need to manipulate. Please describe the situation and your goals. – Mr.Wizard Feb 09 '14 at 16:45SymbolName @ Unevaluated[var]andToString @ HoldForm[var]and each of these may be useful, again depending on your actual needs. I await your clarification. – Mr.Wizard Feb 09 '14 at 16:49Unevaluated. As MrW said, you can use something similar toToString@Unevaluated[1+1]. It gives"1 + 1". Juggling unevaluated expressions is likely to get complicated though ... – Szabolcs Feb 09 '14 at 16:56Unevaluatedmakes it impossible to look up the value ofi[[1]], which isvar1. So I do need the first level of evaluation that replacesi[[1]]withvar1, I just don't want Mathematica to further replacevar1with its value. – verse Feb 09 '14 at 16:58"heading-name" -> variablethat your code takes as input. Is this correct? – Szabolcs Feb 09 '14 at 17:09mappingscome from and is it wrapped inHoldnow to prevent evaluation? – Mr.Wizard Feb 09 '14 at 17:10fn[mappings_]:= Do[ i[[1]]->i[[2]]/.remoteData, {i,mappings} ], and setSetAttributes[fn,HoldAll]. – verse Feb 09 '14 at 17:12fnI myself decide which variable I want to map to which fields in the db. Somappingsare defined by the end-user. – verse Feb 09 '14 at 17:14fn(as parametermappings)? – Mr.Wizard Feb 09 '14 at 17:14remoteData? We (or at least I) need to see the bigger picture here. – Mr.Wizard Feb 09 '14 at 17:15remoteDatawhenever the server informs it of any changes. The data on the server is stored in MongoDB in JSON format, and then imported as a standard Mathematica list usingImportString[..., "JSON"]– verse Feb 09 '14 at 17:18