# How can I create a variable named from a concatenation of strings and assign it a value created similarly? [duplicate]

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

For[i=1,i<=3,i++,
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?

• You can wrap Symbol[...] with Evaluate. Or use With. In any case you probably don't want to use For – b3m2a1 Sep 6 '17 at 1:13
• 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\"$$." – user51827 Sep 6 '17 at 1:22
• That means it's already been assigned to. Alternatively, use Evaluate@ToExpression["The"<>a[[i]], StandardForm, HoldPattern] – b3m2a1 Sep 6 '17 at 1:24
• That works. Many thanks! :) – user51827 Sep 6 '17 at 1:25