# \[?\] Avoid problems with generating symbols?

**URL:** <https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880>\
**Category:** Questions\
**Tags:** computer-science, symbolic-computations\
**Created:** [January 28, 2018, 2:53pm UTC](https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880 "2018-01-28T14:53:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericjohnstone](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@ericjohnstone](https://community.wolfram.com/u/ericjohnstone)\
**Post date:** [January 28, 2018, 2:53pm UTC](https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880/1 "2018-01-28T14:53:03Z")

</div>

Dear MMAers:

Generate a symbol:

```
In[255]:= Symbol["x" <> "1"]

Out[255]= x1

```

Assign a value:

```
In[256]:= Symbol["x" <> "1"] = 45

During evaluation of In[256]:= Set::write: Tag Symbol in Symbol[x1] is Protected. >>

Out[256]= 45

```

Start over:

```
In[264]:= Remove["`*"];
Evaluate[Symbol["x"<>"1"]]=45
Out[265]= 45

```

This seems to work:

```
In[266]:= Symbol["x" <> "1"]

Out[266]= 45

```

Now try to get rid of x1:

```
Remove["x" <> "1"]

Remove::ssym: x<>1 is not a symbol. >>

```

Oh, well. Try to assign again to x1:

```
In[268]:= Evaluate[Symbol["x" <> "1"]] = 78

During evaluation of In[268]:= Set::setraw: Cannot assign to raw object 45. >>

Out[268]= 78

```

Try the symbol directly:

```
In[269]:= Symbol["x"<>"1"]=78
During evaluation of In[269]:= Set::write: Tag Symbol in Symbol[x1] is Protected. >>
Out[269]= 78

```

Maybe Unprotect?

```
In[270]:= Unprotect[Symbol["x" <> "1"]]

During evaluation of In[270]:= Protect::pssl: Symbol[x<>1] is not a string, symbol, or list of strings and symbols. >>

Out[270]= Unprotect[Symbol["x" <> "1"]]

```

Can someone offer advice on generating symbols? I especially would like to use a strange character such as a star so that I can remove the generated symbols en masse.  
Thanks,  
Eric

---

<div class="post-metadata">

**Author:** ![kubapod](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kubapod/32/18662_2.png) [@kubapod](https://community.wolfram.com/u/kubapod)\
**Post date:** [January 28, 2018, 5:50pm UTC](https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880/2 "2018-01-28T17:50:51Z")

</div>

This and more in: [How to create symbols from strings and set values for them?](https://mathematica.stackexchange.com/q/783/5478)

Shortly

```
ToExpression["x1", StandardForm, Function[sym, sym = 32, HoldAll]]

```

Because in

```
Evaluate[Symbol["x1"]] = 1

```

The left hand side is evaluated and you are effectively doing `1=1`. Unprotecting won’t help.

When in doubt, Trace:

```
Trace[Evaluate[Symbol["x1"]] = 1 ]

```

p.s. `Remove` is `HoldAll` and it does not know what to do with `StringJoin` you are giving it. This time the fix is quick:

```
Remove[Evaluate["x" <> "1"]]

```

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [January 28, 2018, 6:15pm UTC](https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880/3 "2018-01-28T18:15:30Z")

</div>

Eric,

For what you are doing I would recommend creating a base name for your “temporary” variables so you can do a wildcard remove and get all of them.

```
In[12]:= Evaluate[Symbol["tempX" <> "1"]] = 45

Out[12]= 45

In[13]:= ?tempX1

Global`tempX1

tempX1=45

In[14]:= Remove["tempX*"]

In[15]:= ?tempX1

During evaluation of In[15]:= Information::notfound: Symbol tempX1 not found.

```

Regards,

Neil

---

<div class="post-metadata">

**Author:** ![ericjohnstone](https://avatars.discourse-cdn.com/v4/letter/e/f0a364/32.png) [@ericjohnstone](https://community.wolfram.com/u/ericjohnstone)\
**Post date:** [January 28, 2018, 11:15pm UTC](https://community.wolfram.com/t/avoid-problems-with-generating-symbols/11880/4 "2018-01-28T23:15:23Z")

</div>

Kuba,

The final line of your post solved my problem. Here is the test function that saves state for different names.

```
test[name_String, init_Integer] := Block[{state},
  If[NameQ[name <> "$"],
   (state = Symbol[name <> "$"]; Print["name known"]; 
    Remove[Evaluate[name <> "$"]]),
   (state = init; Print["name unknown"])];

  state += 50;

  Evaluate@Symbol[name <> "$"] = state
]

```

I found that an easy way to deal with symbols is to remove them and then re-create them. My problem was that the StringJoin wouldn’t work in Remove.

I’ll check out the StackExchange posts. Looks like you’ve done some serious work on this problem.

Neil, yes, that’s what I’m aiming at.

Thanks, guys, Eric
