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.

Say I have a string list called fullpara

fullpara={"width", "long", "line", "distance"}

And there are corresponding variables to each string, and I want to Clear these variables

Clear[width, long, line, distance]

How to do it in a way to manipulate fullpara as a whole?

Clear@@(ToExpression/@fullpara)

obviously won't work because ToExpression will evaluate the variable that already has a value.

share|improve this question
1  
You don't have to, try Clear @@ fullpara or ClearAll.... –  Kuba Nov 25 '13 at 10:27
    
@Kuba I like your quick and accurate answer! –  matheorem Nov 25 '13 at 10:28
1  
@Kuba even if it is closed an answer may still help future visitors (if the title is changed to reflect the actual problem, which is about more about Clear than evaluation as such. –  Yves Klett Nov 25 '13 at 10:54
    
@YvesKlett I don't know how to write a proper title. Could you help to edit my title? Thank you –  matheorem Nov 25 '13 at 11:30
    
@Kuba perhaps relevant: meta.mathematica.stackexchange.com/questions/1101/… –  Yves Klett Nov 25 '13 at 11:53
show 3 more comments

2 Answers 2

up vote 9 down vote accepted

According to the documentation of Clear or ClearAll it is possible to provide symbols in form of regular expression (limited), in particular as string with exact symbol name.

Clear @@ {"width", "long", "line", "distance"}

Let's say there is no possibility to do that, one way would be:

Map[Clear, 
    ToExpression[{"width", "long", "line", "distance"}, InputForm, Hold], 
    {2}]; // ReleaseHold
share|improve this answer
add comment

As Kuba notes you can use strings in Clear directly. However having an understanding of how to work with such problems is helpful so here are some other ways.

A flexible approach is to use MakeExpression to convert strings to expressions in held form (HoldComplete specifically):

MakeExpression[fullpara]
{HoldComplete[width], HoldComplete[long], HoldComplete[line], HoldComplete[distance]}

You can use Apply Join to put these in a single HoldComplete head and then Apply Clear or ClearAll:

Join @@ MakeExpression[fullpara]
HoldComplete[width, long, line, distance]
Clear @@ Join @@ MakeExpression[fullpara]  (* clears Symbols *)

A more specific approach is to use the third argument of ToExpression to Clear individual Symbols directly:

ToExpression[fullpara, StandardForm, Clear]; (* clears Symbols *)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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