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 stalled with the following problem: I want two lists, for example:




The list "namec" is a list with extra information for internal use in a (non-comercial) package, while the List "name" is a vector shown to the user. The "name" inside the list has


I tried the following piece of code:

 g = {1, 2, 3};
 c = {x, y, z};
 (ToExpression["Set[" <> # <> "c" <> ", {g,c," <> # <> "}]"]; 
 ToExpression["Set[" <> # <> ", g]"]) &["name"];

the problem is that with the second assignment, the full list "name" replace the string "name" in the list "namec" (I know it could be a little confusing, this is counterintuitive to me). With the code above I get:




Without the second assignment in the piece of code shown above, I get the list "namec" I want, however, the problem appears when I added the second assignment (name=g). Actually "name" is obtained interactively with an Input, but I simplified the code here. How can I solve this problem?, I suspect that I must use the Hold attribute but what I tried didn't work. Can somebody help me ?

Just for reference the real piece of code is:

With[{symbol = Input["enter a new name"]},
nom = ToString[symbol];
"Set[" <> ToString[symbol] <> "c" <> ", {g,c," <> 
ToString[symbol] <> "}]"];
ToExpression["Set[" <> ToString[symbol] <> ", g]"]; 

(* finally I get the solution based on a suggestion of george2079 (I only modified the last line of his solution) :*)

In[68]:= name = InputString["enter symbol name"]
ToExpression[name <> "={1,2,3}"]
ToExpression[name <> "c" <> "={{1,2,3},{x,y,z},name}"]

Out[68]= "ju"

Out[69]= {1, 2, 3}

Out[70]= {{1, 2, 3}, {x, y, z}, "ju"}

In[71]:= ju

Out[71]= {1, 2, 3}

In[72]:= juc

Out[72]= {{1, 2, 3}, {x, y, z}, "ju"}
share|improve this question
I don't know how to prevent the evaluation of a Symbol entered in an Input[] dialog. Would it be acceptable to enter it in quotes i.e. "name", or use an InputField? – Mr.Wizard Jul 31 '13 at 22:19
Mr.Wizard, sorry for the delay, I just came back. An InputField is more appropriate, since it must be a name given by the user. – Gluoncito Jul 31 '13 at 23:28
Mr Wizard, entering the names in quotes with InputField or Input can be equally acceptable. – Gluoncito Aug 1 '13 at 1:08
I'm sorry, I am out of time today to work on this but if someone doesn't do it first I'll do my best to give you a solution tomorrow. – Mr.Wizard Aug 1 '13 at 1:14
up vote 1 down vote accepted

maybe do this..:

name = {1, 2, 3};
namec = {{1, 2,3}, {x, y,z}, ToString@Unevaluated@name}

(*{{1,2, 3}, {x, y,z}, "name"}*)

or simply

namec = {{1, 3}, {x, y}, "name"}

Then when you want to access it..


(*{1, 2, 3}*)

Edit: use input string..

name = InputString["enter symbol name"]
ToExpression[name <> "={1,2,3}"]
namec = {{1, 2, 3}, {x, y, z}, name}

({1, 2, 3})

share|improve this answer
@Mr.Wizard: Thank you. What I want to do is a little more complicated, in this way I cannot use your suggestion, I modified the code shown above accordingly but it didn`t work. I'll try to specify better my question – Gluoncito Jul 31 '13 at 21:33
george2079: thank you, the "name" assignment is passed interactively, I edited my question above. So, I cannot use simple lists constructions. – Gluoncito Jul 31 '13 at 21:37
@Gluoncito Try using InputString[] instead.. – george2079 Aug 1 '13 at 20:59
(at)george: It don't work, if I enter say "ve", when I input "vec" I do not recover the list namec defined above. However, thank you for responding. – Gluoncito Aug 1 '13 at 21:16
(at)george: it worked with a slight modification, I'll paste the solution below the question and give you the credit. Thank you. – Gluoncito Aug 1 '13 at 21:30

There seems to be more than one issue here. The primary one is the storage format of the namec expression; you need to hold name somehow or it will evaluate. You do not explain how you will use it, so I will just use Hold:

name = {1, 2, 3};
namec = Hold[{1, 2, 3}, {x, y, z}, name];

Hold[{1, 2, 3}, {x, y, z}, name]

You could use Hold[{{1, 2, 3}, {x, y, z}, name}] if you prefer, or HoldForm, or several other methods.

You say "Head[name]=String" but it's not clear to me what you mean or want. If you have "name", which is a string, and you want to use it to create the expression shown above then please see:
How to pass a symbol name to a function with any of the Hold attributes?

Perhaps you want the namec expression to immediately evaluate to {{1, 2, 3}, {x, y, z}, (* current contents of name *)} when it is called. Here is an example of that:


from[s_String, g_, c_] :=
 MakeExpression @@ {{s, s <> "c"}} /. {_[sym_], _[lhs_]} :> (sym = g; lhs := {g, c, sym})

g = {1, 2, 3};
c = {x, y, z};
from["name", g, c]

Check the definitions that were made:



Note that name exists unevaluated in the definition of namec.

If you change name then call namec you get the current value:

name = {4,5,6};

{{1, 2, 3}, {x, y, z}, {4, 5, 6}}
share|improve this answer
@Gluoncito I still don't know if I understand but I'm trying to help. Please take a look at my answer and tell me if we're getting closer. – Mr.Wizard Jul 31 '13 at 21:32
I had no problems in creating the list "namec", with the string "name" inside, the problem in the code above is when I add name=g, I cannot use simple lists constructions because I get the name with an Input inside a With. I'm afraid that putting the larger code in the question could make it harder to understand what I want. Thanks for all the help I`m learning a lot. – Gluoncito Jul 31 '13 at 21:45
(at) Mr Wizard: thanks for the code above, but if I run it when I input "namec" I get the same thing I got: "{{1,2,3},{x,y,z},{1,2,3}}". – Gluoncito Jul 31 '13 at 22:00
@Gluoncito I think it would be best if you included the full code at the bottom. Regarding your second comment that's actually intended; internally the symbol name in the definition of namec is unevaluated, and therefore changes to name will be propagated to namec as well; this was my best guess as to the behavior you want. Since it was not correct please do your best to describe the behavior that you want. (That is: "What are you really trying to do?") – Mr.Wizard Jul 31 '13 at 22:04

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.