1

I am stalled with the following problem: I want two lists, for example:

name={1,2,3}

and

namec={{1,2,3},{x,y,z},name}

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

Head[name]=String

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:

 name={1,2,3}

and

 namec={{1,2,3},{x,y,z},{1,2,3}}

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];
ToExpression[
"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"}
Gluoncito
  • 517
  • 3
  • 13
  • 1
    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 01 '13 at 01: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 01 '13 at 01:14

1 Answers1

1

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

Symbol@Last@namec

(*{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}
Symbol@Last@namec

({1, 2, 3})

george2079
  • 38,913
  • 1
  • 43
  • 110
  • @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 01 '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 01 '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 01 '13 at 21:30