How to Clear variables represented as a list of strings?
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
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 *)