How to name a variable with the value of another variable?
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:
Assigning values to a list of variable names
Elegant manipulation of the variables list
How to 'merge' a list like FromDigits, but with a mixture of numbers and symbols?
How do you programatically load data into symbols? (especially Kuba's answer)
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