9
$\begingroup$

A simple but very important (at least for me) question. In some languages for defining a variable, for example, we can do:

i=10

"d"&i=30

So I have defined the variable d10, and...

d10=30

That is to say... I've named a variable (d10) as as function of an other variable (i).

How can I do this in Mathematica? Sorry if the title is bad.

Regards!

$\endgroup$
  • $\begingroup$ Try with Symbol["name"]: "refers to a symbol with the specified name." $\endgroup$ – unlikely Aug 1 '14 at 18:57
  • 3
    $\begingroup$ Maybe there are shorter idioms for this, but one way is: Evaluate[ToExpression["d"~~ToString[i]]] = 30 $\endgroup$ – Daniel Lichtblau Aug 1 '14 at 19:08
  • 1
    $\begingroup$ I'm wondering if you want x[i]? It's not exactly what you asked for; but if you don't know you can use x[i] as a variable, which represents x[10] when i is 10, then you might not know to ask for it. $\endgroup$ – Michael E2 Aug 1 '14 at 19:13
  • $\begingroup$ Daniel, your suggestion works very well. Thanks a lot. $\endgroup$ – Luis Fernando Moura Aug 1 '14 at 19:26
  • 1
    $\begingroup$ @Luis Be aware that Daniel's simple suggestion will not allow reassignment. (As I'm sure he knows.) I again updated my answer to make clear that the method I gave does allow reassignments. $\endgroup$ – Mr.Wizard Aug 1 '14 at 19:40
8
$\begingroup$

I shall assume that you want a compact syntax to make this practical to use.
I shall choose cs, standing for compound symbol:

cs[x__] :=
  ToHeldExpression @ ToString @ Row @ {x} /.
    {_[s_Symbol] :> s, _ :> $Failed}

func_[a___, Unevaluated @ cs[x__], b___] ^:= 
  ToHeldExpression @ ToString @ Row @ {x} /.
    {_[s_Symbol] :> func[a, s, b], _ :> $Failed}

Test:

i = 10;
cs["d", i] = 30;

d10
30

Any expressions can be used so long as their evaluated forms concatenate to a valid Symbol name:

cs[Pi, d10, "x"] = 86;

Pi30x
86

Reassignment is possible:

cs[Pi, d10, "x"] = 99;

Pi30x
99

If an invalid Symbol name is produced by the concatenation $Failed is returned:

cs[5, "x"] = 30
$Failed

(Symbol names cannot start with numbers.)


Recommended reading:

$\endgroup$
3
$\begingroup$

Despite the accepted excellent answer by Mr.Wizard I think it is worth to point out that the standard idiomatic approach to the problem in Mathematica is to use indexed variables:

In[1]:= i = 10;
d[i] = 30;
Definition[d]
d[10] = 10;
Definition[d]
Out[3]= d[10] = 30

Out[5]= d[10] = 10
$\endgroup$
  • 1
    $\begingroup$ A brief, but related discussion: When to use indexed variables. I know the advantages/disadvantages of indexed variables have been discussed here and there on the site, but I'm having trouble locating relevant q & a. (+1) $\endgroup$ – Michael E2 Aug 2 '14 at 13:31
  • $\begingroup$ For what it's worth this is covered in the A Better Alternative section at the top of my answer to the first linked question in my answer above. $\endgroup$ – Mr.Wizard Aug 2 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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