# Associating a string variable with a numeric variable

There is a translation operation from a numeric variable to a string variable:
a[i_, j_] := Symbol["a" <> ToString[i] <> ToString[j]]

How could the reverse operation be done? Something like that:
Symbol["a" <> ToString[i_] <> ToString[j_]] := a[i, j]

• Why on earth would you want to do that? May 20 at 12:17
• I am getting a headache just trying to understand what you are trying to do :) Are you trying to convert a12 to a[1,2] for example? can you give an explicit example of an input and what the output should be? May 20 at 12:24
• Anyways, ReleaseHold[With[{symbol = Symbol["a" <> ToString[i] <> ToString[j]]},HoldForm[symbol = a[i, j]]] ] might work. May 20 at 12:25
• @Nasser, I have many string variables, I want to somehow automate data entry for them. But how can I distinguish them from each other in order to apply a loop to them? May 20 at 12:48
• I've read all of your recent questions but I'm also confused. I have a strong feeling that you're asking XY problem repeatedly. What on earth are you trying to design? May 20 at 13:17

As with all the others, I do not understand what are you after. However, in the past, I met a comparable problem myself, where I had a good reason to do something like this. Of course, without a clear understanding of your aim, I can give a useless solution. Anyway, let us try.

You defined the function which acts only on tensors with the head a transforming them into a string and then into a symbol:

a[i_, j_] := Symbol["a" <> ToString[i] <> ToString[j]]


Let us check

a[i, j] // FullForm
a[i, j] // Head


returns aij and Symbol correspondingly. That's OK.

Let us introduce the function

toTensor[expr_Symbol] :=
Module[{x, y, z},
ToExpression@Characters[ToString[expr]] /. {x_, y_, z_} :> x[y, z]]


Now let us apply it to aij:

Clear[a];
toTensor[aij]

(* a[i, j] *)


The key point here is to first apply Clear[a]. It is because you have already previously defined a[i,j] such that it immediately transforms into aij. Therefore, without clearing it will always return aij. The symbol with a different first letter you can transform without clearing

toTensor[bij]

(* b[i, j]  *)


Hope this helps.

Have fun!

• Thanks a lot for the explanation! May 20 at 19:37
• Simpler: ToExpression@Characters[ToString[expr]] /. {x__} :> (First@{x})[Sequence @@ Rest@{x}] May 20 at 22:05

I feel like we're going in circles. Over the last 2 days you've asked several questions about associating atomic symbols with headed expressions in some fashion. I'm almost certain that you don't need to do any of this. But I can't help you figure out what to do instead without knowing what problem you're actually trying to solve. And I don't mean the ultimate problem, like whatever advanced math or physics problems you're dealing with. I just mean the computational problem you're struggling with. Can you give an example that shows a bit more of the problem than you've shown us so far?

In the meantime, I'll take a stab in the dark...

Maybe want you want is to use a data structure rather than individual atomic values. You keep mentioning loops, so instead of things like a[1,2] <--> a12, maybe you use Part instead: a[[1,2]]. So, you can do things like this:

a = {{1, 2}, {3, 4}};
a[[1, 2]]
(* 2 *)


Or loops:

Do[
Do[
Print[a[[i, j]]],
{j, 2}],
{i, 2}]


Or maps:

Map[f, a, {2}]
(* {{f[1], f[2]}, {f[3], f[4]}} *)


Or threading, or matrix operations, or etc etc.