This question already has an answer here:

I have got a list of strings,

a = {"first","second","third"}.

For each of those strings I would like to create a variable called "The" followed by its content, so Thefirst, Thesecond, and Thethird. And in each case I would like to assign the variable the value of a pathname of the form DriveLetter:/doc/myfirst.txt, etc., where Driveletter is a previously created variable holding, let us say, the value "C:\".

So to be even clearer, this is what I want to get:

Thefirst = "C:\doc\myfirst.txt"
Thesecond = "C:\doc\mysecond.txt"
Thethird = "C:\doc\mythird.txt"

I've tried

Symbol["The" <> a[[i]]] = 
FileNameJoin[{DriveLetter, "doc/", StringJoin["my", a[[i]],".txt"]}]]

but this gives the error message

Set::write: Tag Symbol in Symbol[Thefirst] is Protected.

How can I achieve the goal?


marked as duplicate by Kuba Sep 6 '17 at 5:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ You can wrap Symbol[...] with Evaluate. Or use With. In any case you probably don't want to use For $\endgroup$ – b3m2a1 Sep 6 '17 at 1:13
  • $\begingroup$ Thanks, but replacing Symbol["The" <> a[[i]]] with Evaluate[Symbol["The" <> a[[i]]]] I get Set::setraw: "Cannot assign to raw object \!\(\"C:\\\\doc\\\\myfirst.txt\"\)." $\endgroup$ – user51827 Sep 6 '17 at 1:22
  • 1
    $\begingroup$ That means it's already been assigned to. Alternatively, use Evaluate@ToExpression["The"<>a[[i]], StandardForm, HoldPattern] $\endgroup$ – b3m2a1 Sep 6 '17 at 1:24
  • $\begingroup$ That works. Many thanks! :) $\endgroup$ – user51827 Sep 6 '17 at 1:25