# How to implement the for loop to replace text strings or texts?

I wonder define a bunch variables named like $X_1$, $X_2$, $...$, $X_n$. Is there some smart way to declare them instead of by pure typing. One trick I can image is to use the for loop,For [i = 1, i <= n, i++], to replace the subindices,. However, now each i is not numbers but texts, I don't know how to implement the for loop to texts or text strings. Can anyone give me some hints?

• Please avoid using subscripts. Some feferences: ref1, ref2, ref3 – Dr. belisarius Oct 20 '15 at 11:40

This would be more idiomatic to Mathematica than using a for loop:

indices = {"a", "b", "c"};

Array[(x[indices[[#]]] = #) &, 3];

{x["a"], x["b"], x["c"]}


{1, 2, 3}

This is an equivalent for loop:

For[i = 1, i <= Length[indices], i++,
x[indices[[i]]] = i]

• I'd have used Scan[] myself… – J. M.'s technical difficulties Oct 20 '15 at 10:10
• @J.M. -- Can you elaborate for the sake of posterity? – Jagra Oct 20 '15 at 13:16
• @Jagra Scan[(x[#] = #) &, indices] – Dr. belisarius Oct 20 '15 at 13:39