Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I'm trying to create several symbols and assign values to them. Here's what currently appears to work:

Evaluate[Symbol["fdds" <> ToString[10]]] = 1

But there's then another problem: I can't clear it without explicitly saying fdds10, i.e. this code

Evaluate[Symbol["fdds" <> ToString[10]]] =.

as well as this

Clear[Evaluate[Symbol["fdds" <> ToString[10]]]]

give errors: the former gives

Unset::usraw: Cannot unset raw object 1. >>

and the latter results in

Clear::ssym: 1 is not a symbol or a string. >>

How do I create good easily manageable symbols?

PS: I know I could use something like fdds[[10]] here, but I'm interested in symbol-based solution.

share|improve this question

marked as duplicate by Michael E2, Öskå, bobthechemist, Sjoerd C. de Vries, m_goldberg Jun 29 at 17:35

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.

add comment

3 Answers 3

up vote 5 down vote accepted

You can apply Clear to the result of the expression generated by the string by using the optional third argument of ToExpression:

Evaluate[Symbol["fdds" <> ToString[10]]] = 1

(* ==> 1 *)

fdds10

(* ==> 1 *)

ToExpression["fdds" <> ToString[10], InputForm, Clear]

fdds10

(* ==> fdds10 *)

The third argument of ToExpression is Clear and wraps the expression fdds10 before it is evaluated.

share|improve this answer
    
What would be the principal difference between Symbol and ToExpression here if the latter is used just with one argument? Could I just replace the former with the latter for the first assignment? –  Ruslan Jun 28 at 19:35
    
@Ruslan ToExpression is just a more high-level function that can act on expressions (even lists of expressions). The strings accepted by Symbol are limited to be valid symbol names. Because of this relationship, you can definitely use ToExpression in place of Symbol when you're sure you've constructed a valid symbol name string. –  Jens Jun 28 at 19:43
add comment

Clear works on Strings exactly for such cases (I believe).
You must still evaluate StringJoin, my favorite way being Apply:

Clear @@ {"fdds" <> ToString[10]}

If this is all you need the question should be marked as a duplicate of:

If we wish to make the question unique we can look at the more difficult case of Unset as applied to other kinds of definitions (those besides OwnValues). For example:

Evaluate[Symbol["foo" <> ToString[10]][x_?Negative]] = 1;
Evaluate[Symbol["foo" <> ToString[10]][x_]] = 2;

How to Unset the first definition leaving the second in place?

These definitions work:

foo10[-3]
foo10[5]
1
2

However, due to the generic nature of the second definition this will evaluate in a way we do not want:

Evaluate[Symbol["foo" <> ToString[10]][x_?Negative]]   (* Evaluate merely for consistency *)
2

We can evaluate the Symbol foo10, since here it does have a value assigned, and insert it with Function:

Unset[#[x_?Negative]] & @ Symbol["foo" <> ToString[10]]

Definition[foo10]
foo10[x_] = 2

Also related:

share|improve this answer
add comment

I don't understand exactly what your application is, but looking at your usage I would guess, you don't want fdds[[10]] as you would need to initialize an array?

fdds[[10]] // FullForm
Part[fdds,10]

In this case you could use fdds[10]. Note that [[]] is not [] and you do not need to initialize anything.

fdds[10] // FullForm
fdds[10]
fdds[5] = 1;
fdds[10] = 2;
fdds // Definition
fdds[5]=1
fdds[10]=2
Unset[fdds[10]]
fdds // Definition
fdds[5] = 1

edit: Now I know, this is answer is not suitable for OPs case, but maybe the answer helps someone new to Mathematica to avoid messy method.

share|improve this answer
    
Please note the PS part of the question, which has been there since the very beginning. I do know I can do this like an array, but I'm interested in symbol-based approach. –  Ruslan Jun 28 at 21:42
    
@Rusian It is not an array. [] is not [[]]. And I don't know what do you mean by "symbol-based". Do you need, that the Head is a Symbol? Could you tell why? –  Johu Jun 28 at 21:50
    
I was just interested in creating a usable symbols. I'm well aware of the possibility to use hash maps like fdds[anything]=1. –  Ruslan Jun 29 at 6:04
    
@Ruslan. Ok. Now you told we words. I hope the answer still helps someone else to avoid messy methods. –  Johu Jun 29 at 12:00
add comment

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