Well, if they are really called a1
, a2
, etc, you could do this:
Assign[x_, y_] := Module[{s1, s2, n, sn},
s1 = SymbolName[Unevaluated[x]];
s2 = SymbolName[Unevaluated[y]];
For[n = 1, n <= Length[x] && n <= Length[y], n++,
sn = ToString[n];
Evaluate[Symbol[s1 <> sn]] = Evaluate[Symbol[s2 <> sn]]
]
]
SetAttributes[Assign, HoldAll]
And then
Clear[b1, b2, b3];
Clear[a1, a2, a3];
a = {a1, a2, a3}
b = {b1, b2, b3}
Assign[a, b]
a
Gives the results for a
, b
and a
again as:
{a1, a2, a3}
{b1, b2, b3}
{b1, b2, b3}
As expected.
In general you can create expressions like these from proper use of SymbolName
and Symbol
, but be careful with your evaluation. If I had written SymbolName[x]
(without the Unevaluated
) then it would've interpreted that as SymbolName[{a1, a2, a3}]
, which is clearly not desirable. Not using Evaluate
on Symbol[s1 <> sn]
will have Mathematica complain that you're trying to reassign the Symbol
function.