Re: Convert from String to variable
- To: mathgroup at smc.vnet.net
- Subject: [mg70241] Re: [mg70229] Convert from String to variable
- From: Bruce Colletti <vze269bv at verizon.net>
- Date: Mon, 9 Oct 2006 01:55:55 -0400 (EDT)
Peng Try this for array variables x[y,z,...]. Mathematica 5.2. Bruce ------------ xconvert@X_List := Map[ToExpression@ StringJoin[ToString@Head@#, Sequence[ToString /@ (List @@ #)]] &, X]; (* Example #1 *) X = Flatten@Array[a, {3, 3, 3}, {0, 0, 0}]; MapThread[#1 -> #2 &, {X, xconvert@X}] (* Example #2 *) X = {z[4, 5, 6, 7], y[0]}; MapThread[#1 -> #2 &, {X, xconvert@X}] Out[50]= {a[0, 0, 0] -> a000, a[0, 0, 1] -> a001, a[0, 0, 2] -> a002, a[0, 1, 0] -> a010, a[0, 1, 1] -> a011, a[0, 1, 2] -> a012, a[0, 2, 0] -> a020, a[0, 2, 1] -> a021, a[0, 2, 2] -> a022, a[1, 0, 0] -> a100, a[1, 0, 1] -> a101, a[1, 0, 2] -> a102, a[1, 1, 0] -> a110, a[1, 1, 1] -> a111, a[1, 1, 2] -> a112, a[1, 2, 0] -> a120, a[1, 2, 1] -> a121, a[1, 2, 2] -> a122, a[2, 0, 0] -> a200, a[2, 0, 1] -> a201, a[2, 0, 2] -> a202, a[2, 1, 0] -> a210, a[2, 1, 1] -> a211, a[2, 1, 2] -> a212, a[2, 2, 0] -> a220, a[2, 2, 1] -> a221, a[2, 2, 2] -> a222} Out[52]= {z[4, 5, 6, 7] -> z4567, y[0] -> y0} ===================== From: "PengYu.UT at gmail.com" <PengYu.UT at gmail.com> To: mathgroup at smc.vnet.net Subject: [mg70241] [mg70229] Convert from String to variable I want generate some code like below. {a[0][0][0] -> a000, a[0][0][1] -> a001, a[0][0][2] -> a002, a[0][1][0] -> a010, a[0][1][1] -> a011, a[0][1][2] -> a012, a[0][2][0] -> a020, a[0][2][1] -> a021, a[0][2][2] -> a022, a[1][0][0] -> a100, a[1][0][1] -> a101, a[1][0][2] -> a102, a[1][1][0] -> a110, a[1][1][1] -> a111, a[1][1][2] -> a112, a[1][2][0] -> a120, a[1][2][1] -> a121, a[1][2][2] -> a122, a[2][0][0] -> a200, a[2][0][1] -> a201, a[2][0][2] -> a202, a[2][1][0] -> a210, a[2][1][1] -> a211, a[2][1][2] -> a212, a[2][2][0] -> a220, a[2][2][1] -> a221, a[2][2][2] -> a222} I'm looking for an automated way to generate the assignments for a[i][j][k] (i = 1...n,j=1...n,k=1...n). One way that I think of is Flatten[ Table[a[i][j][k] -> "a" <> ToString[i] <> ToString[j] <> ToString[k], {i, 0, 2}, {j, 0, 2}, {k, 0, 2}]]. But this method requires to convert the strings to variables. Is it possible to do this in Mathematica? Are there any better solution to generate the assignment list? Thanks, Peng