1
$\begingroup$

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"}
$\endgroup$
4
  • 1
    $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Jul 31, 2013 at 22:19
  • $\begingroup$ 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. $\endgroup$
    – Gluoncito
    Jul 31, 2013 at 23:28
  • $\begingroup$ Mr Wizard, entering the names in quotes with InputField or Input can be equally acceptable. $\endgroup$
    – Gluoncito
    Aug 1, 2013 at 1:08
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Aug 1, 2013 at 1:14

1 Answer 1

1
$\begingroup$

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})

$\endgroup$
5
  • $\begingroup$ @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 $\endgroup$
    – Gluoncito
    Jul 31, 2013 at 21:33
  • $\begingroup$ george2079: thank you, the "name" assignment is passed interactively, I edited my question above. So, I cannot use simple lists constructions. $\endgroup$
    – Gluoncito
    Jul 31, 2013 at 21:37
  • $\begingroup$ @Gluoncito Try using InputString[] instead.. $\endgroup$
    – george2079
    Aug 1, 2013 at 20:59
  • $\begingroup$ (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. $\endgroup$
    – Gluoncito
    Aug 1, 2013 at 21:16
  • $\begingroup$ (at)george: it worked with a slight modification, I'll paste the solution below the question and give you the credit. Thank you. $\endgroup$
    – Gluoncito
    Aug 1, 2013 at 21:30

Your Answer

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

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