Message Boards Message Boards

GROUPS:

[✓] Avoid problems with generating symbols?

Posted 1 year ago
1834 Views
|
3 Replies
|
6 Total Likes
|

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

3 Replies

This and more in: How to create symbols from strings and set values for them?

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"]]

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

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

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract