-1
$\begingroup$

I have a bunch of symbols which are collected as a list inside a variable within a bit of code e.g.

a=1;b=2;
par={a,b};

I want to clear the values of both a and b using only a reference to par. Here is my attempt and the error produced:

Clear[Sequence@@par]
... Clear::ssym: Sequence@@par is not a symbol or a string.

I can understand why this gives an error but can't figure out a way out of it.

Thanks for your help

$\endgroup$
  • 1
    $\begingroup$ par is simply {1, 2} here, how would it know about the symbols a and b? $\endgroup$ – Marius Ladegård Meyer Mar 31 '17 at 9:47
  • $\begingroup$ To amplify Marius's comment, look at the result of ??par. $\endgroup$ – J. M. is away Mar 31 '17 at 9:48
  • $\begingroup$ I do get the idea. Is there a way, though ? $\endgroup$ – jrekier Mar 31 '17 at 9:50
  • $\begingroup$ An approach similar to this is one possibility: par = Hold[a, b]; and much later, Scan[Clear, par] or Clear @@ par. $\endgroup$ – J. M. is away Mar 31 '17 at 10:00
0
$\begingroup$

This will not work. Because you used Set, not SetDelayed. After executing this code:

a = 1; 
b = 2; 

par = {a, b}
OwnValues[par]

(*
    {1, 2}
    {HoldPattern[par] :> {1, 2}}
*)

par isn't a list with a and b. par is a list that contains {1, 2}.

For your task you can use this code:

a = 1; 
b = 2; 

par := {a, b}
par
OwnValues[par]

(* 
    {1, 2}
    {HoldPattern[par] :> {a, b}} 
*)

Now you must take list of values from OwnValues rule:

Hold[par] /. OwnValues[par]

(* Hold[{a, b}] *)

And if you want clear a and b:

Hold[par] /. OwnValues[par] /. Hold[{args__}] :> Clear[args]

par
{a, b}

(* 
    {a, b} 
    {a, b}
*)

Definition for par preserved and for a and b cleared

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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