I have a list of global variables (some of them are indexed variables), for example:
varsH = Hold[U0[1], U0[2], B0, V0[1], V0[2]]
Or (if it is easier to handle) I can have them as a List
of Strings
:
varsS = {"U0[1]", "U0[2]", "B0", "V0[1]", "V0[2]"}
Now I need to be able to do the following things knowing only the position (i) of the variable in the list:
1. Assign new value to the variable (the variable can already have a value)
2. Clear the variable
3. Get the current value of the variable
4. Get the name of the variable as `String`
Additionally, I need to be able to
5. Clear all the variables at once
What is the most elegant approach to this task? I know, it would be easier to have all the variables just as one indexed variable but I need names of the variables to be explicit.
By the term "elegant" I mean that every of the listed things should be written as short as possible (for a solution without helper functions). If there is no elegant way to avoid use of helper functions, they should be as clear as possible.
P.S. It is known that the variables should always be numerical or undefined.
Scan[Clear, Hold[U0, B0, V0]]
then? $\endgroup$