3

I have a list say:

m = {1, 2, 3}

The list is inside a package, and is the result of a calculation. I want to give the user the option to rename it, for example with an input:

new = Input["enter a new name"]

I have not enough experience, but I tried in several ways and failed. The most obvious failure is to make:

new -> m

I cannot define new as a list prior to this assignment. On the other hand, I cannot define the name of a list as a vector with super-index or sub-index, for example:

v^i_; j = {1, 2, 3}

Can somebody help me?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Gluoncito
  • 517
  • 3
  • 13
  • because I want to rename m with a new name passed with the function Input. Thankx for prompt response. – Gluoncito Jul 23 '13 at 20:48
  • @Nasser: thankx for your response, but it didn`t work: I typed: In[1362]:= m = {1, 2, 3}

    Out[1362]= {1, 2, 3}; l = Input["enter name"]; l=m; Clear[m]; and then (new name is: lista): In[1396]:= lista

    Out[1396]= lista,

    – Gluoncito Jul 23 '13 at 21:18

3 Answers3

4

The following works for me, but I'm not sure it's what you want.

myPkg`m = {1, 2, 3};
newRef = ToString@Input["enter a new name"]

x[1]

ToExpression["Set[myPkg`" <> newRef <> ", myPkg`m]"];
myPkg`x[1]

{1, 2, 3}

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • m_goldberg, thankx a lot, your approach also worked fine, in fact is nearest to what I am looking for. The list m is actually a contravariant vector v^i, on which I calculated the covariant derivative, so I must rename the resulting vector as v^i_;j, as I don`t know how to include this characters (^i_;j) to the name of a symbol I must ask the user to rename the covariant derivative each time. But, this is fine for now. – Gluoncito Jul 23 '13 at 22:04
  • @Gluoncito. Inside your package, have you tried using Subscript[Superscript[v, i], j] = m, where i, and j have appropriate integer values? – m_goldberg Jul 23 '13 at 22:33
  • @Gluoncito. You might also use doubly-indexed variables such as v[2, 3], either in your package or entered into the "enter a new name" dialog. I think these are easier to deal with than nested Subscript and Superscript boxes. – m_goldberg Jul 23 '13 at 22:55
  • 1
    @Gluoncito If m_goldberg's answer is "nearest to what [you] are looking for" feel free to un-accept mine and accept his. I was just thinking about Dialog boxes at the time you asked your question so I figured I'd shove a square peg in a round hole. – bobthechemist Jul 24 '13 at 00:33
  • @m_goldberg: if I define the sub/superscripts with the command above, when the user types v^i_j he/she do not get the vector as an output, I tried, the user must type Subscript[Superscript[v, i], j] to get the vector. – Gluoncito Jul 25 '13 at 03:24
  • Your second suggestion seems more easy to implement. I`ll let the user define the name of a vector say, V, he/she will give its 4 coordinates, and tell if the vector is covariant or contravariant. With another function the user can calculate the covariant derivatives of the vector. In this subroutine, the user can change the name of the resulting vector, or the function just can add a string character, for example, the resulting vector is now called Vdn (without ^ _, which seems problematic for naming arrays). – Gluoncito Jul 25 '13 at 03:24
  • @bobthechemist: no problem I'll surely vote up other answers of m_goldberg, I was impresed with your solutions, most books I read about Mathematica have traditional/procedural programming style and I want to learn the full power of mathematica. thanks to all of you, I am learning a lot!. – Gluoncito Jul 25 '13 at 03:37
2

Do you want a Dialogbox that asks the user to input the name of a variable? It's late in the day for me but here's something crazy:

m = {1,2,3};

(Hold[(f = m)] /. f -> DialogInput[{name = ""}, Column[{"Type a name", 
InputField[Dynamic[name], String], Button["Proceed", DialogReturn[Symbol@name],         
ImageSize -> Automatic]}]]) // ReleaseHold

Entering test in the dialogbox should assign test = {1,2,3}.

bobthechemist
  • 19,693
  • 4
  • 52
  • 138
1

I would do it like this:

With[{symbol = Input["enter a new name"]}, symbol = m]
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • Simon Woods; thankx it worked too, I'm just learning and migrating from fortran! – Gluoncito Jul 23 '13 at 22:17
  • @Gluoncito, good luck! Mathematica's learning curve can be a bit steep to begin with, but it's worth it. Have you seen this question? It's got some really helpful stuff for new users. Item 4 of the "Basic Syntax Issues" answer might be particularly relevant - being able to use sub and superscripts seems like a really nice feature but most experienced users learn to avoid them. – Simon Woods Jul 24 '13 at 09:38
  • thankx for the reference, I'd already read some books about mathematica the last week, but still I'm not confident with the more abstract syntaxis, however, thanks to this forum I am learning faster. – Gluoncito Jul 25 '13 at 03:08