The two following codes work similarly:

Num = 5; 
symbols = Hold[X1, X2, X3, X4, X5]; 


Num = 5; 
Xlists= StringJoin["X", #] & /@ Array[ToString, Num];
symbols = Hold[Xlists[[1]], Xlists[[2]], Xlists[[3]], Xlists[[4]], Xlists[[5]]];


When Num is large (for instance Num=200), I would not like to use the stupid way: write all the X1, X2, ..., X200 in the Hold[].

I wonder whether there are something like Hold[Xlists] for such problem. Well, I tried different ways but failed.

Thank you very much in advance!


data = {{0, 0, 0}, {1, 2, 3}, {2, 3, 4}, {4, 5, 6}, {0, 1, 2}};
For[ii = 1, ii <= Num, ii++,
  symbols[[{ii}]] /. _[x_] :> (x = data[[ii]])

I know that the X1,X2,...X5 will have certain values such as X1 will be {0,0,0}. Then it seems that I cannot get the symbol X1 using the way symbols[[1]] because it will return {0,0,0} not X1 (except I directly write as X1).

So after giving values to X1,X2,..., how can I can the it form symbols instead of directly using X1,X2,...

Thank you!

  • $\begingroup$ @xzczd, thank you for pointing out. I have modified it. $\endgroup$ – Marie Apr 28 '20 at 11:57
  • $\begingroup$ When e.g. X1=12345, what behavior is desired? Hold[X1, X2, X3, X4, X5], or Hold[12345, X2, X3, X4, X5]? $\endgroup$ – xzczd Apr 28 '20 at 12:02
  • $\begingroup$ @xzczd, X1 and all the Xn is a symbol which you can give values later. I think it is Hold[X1, X2, X3, X4, X5], and then give values to certain symbol. because I would like to use DumpSave for storing with the related symbol. Since DumpSave doesn't work for something like list[[1]]. $\endgroup$ – Marie Apr 28 '20 at 12:18
  • 1
    $\begingroup$ Then why not Hold @@ (Symbol["X" <> ToString@#] & /@ Range[5])? $\endgroup$ – xzczd Apr 28 '20 at 12:29
  • $\begingroup$ @xzczd, oh, thank you. I don't know this way. $\endgroup$ – Marie Apr 28 '20 at 12:37

You can use the fourth argument of Array as follows:

Array["X" <> ToString@# &, Num, 1, Hold[##] &]
 Hold["X1", "X2", "X3", "X4", "X5"]
Array[Symbol["X" <> ToString@#] &, Num, 1, Hold[##] &]
 Hold[X1, X2, X3, X4, X5] 

If you want to wrap each element with Hold:

Array["X" <> ToString@# &, Num, 1, Hold /@ {##} &]
{Hold["X1"], Hold["X2"], Hold["X3"], Hold["X4"], Hold["X5"]}
Array[Symbol["X" <> ToString@#] &, Num, 1, Hold /@ {##} &]
{Hold[X1], Hold[X2], Hold[X3], Hold[X4], Hold[X5]}
  • $\begingroup$ thank you again, kglr :) so helpful! $\endgroup$ – Marie Apr 28 '20 at 11:58
  • $\begingroup$ @Xuemei, my pleasure. $\endgroup$ – kglr Apr 28 '20 at 12:02
  • $\begingroup$ I edit one small question in the end: if i give values to X1,X2,... and then symbols[[index]] will not return the symbol X1,X2,... as I want but gives me the given values. Could you please have a look? Thank you very much! do you think it is possible? $\endgroup$ – Marie Apr 28 '20 at 13:19
  • $\begingroup$ hey, kglr. the Edit2 part is something like "give values to variable and then get the name (or symbol) of variable" instead writing the variable itself (since there will be many variables in the form of X1, X2, X3,...). Thank you! $\endgroup$ – Marie Apr 28 '20 at 13:42
  • 1
    $\begingroup$ I just thought to use Name["X*"]. thank you anyway :) $\endgroup$ – Marie Apr 28 '20 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.