0
$\begingroup$

I have a variable states that contains a list of held symbols:

states = Table[Hold@Evaluate@Unique[state], {2}]
(* {Hold[state$817], Hold[state$818]} *)

Initializing the valuse of these variables is not really a problem, I can do it as in

Evaluate@ReleaseHold@states[[1]] = Red
Evaluate@ReleaseHold@states[[2]] = Red

But I didn't manage to find a convenient way to replace the values of these symbols after the first initialization. I tried a variety of things, including the Trott-Strzebonski in-place evaluation technique.

I managed to make this almost work, but I still feel like I'm missing something. If I for example try

states[[1]] /. s_Symbol :> RuleCondition[s = Green;];

the value of the variable is correctly reassigned, but I also get the error

Set::wrsym: Symbol Hold is Protected.

which I'm guessing arises from Hold itself matching the s_Symbol condition. But if I try to make Hold not match with

states[[1]] /. s_Symbol?(Head@# =!= Hold &) :> RuleCondition[s = Green;];

the error does not go away.

What's the preferred way to handle this situation?

$\endgroup$
2
  • 4
    $\begingroup$ Hold[var] /. Hold[s_] :> RuleCondition[s = Green;]; $\endgroup$
    – Kuba
    Commented Feb 21, 2017 at 22:57
  • 1
    $\begingroup$ Not sure why you want to use RuleCondition. Just: states[[1]] /. Hold[s_] :> Set[s, Green] should work. $\endgroup$
    – Carl Woll
    Commented Feb 21, 2017 at 23:19

1 Answer 1

2
$\begingroup$

This general operation has been covered before, including:

From my answer to the last question:

func_[bump[lst_, idx___], arg___] ^:= 
  func[#, arg] & @ Part[List @@@ Unevaluated @@ {lst}, {1}, idx]

Applied:

states = Join @@ Table[Hold@Evaluate@Unique[state], {3}]
Hold[state$534, state$535, state$536]
bump[states, 1] = "red";
bump[states, {2, 3}] = {"green", "blue"};

List @@ states
{"red", "green", "blue"}
bump[states, 2] = "black";

List @@ states
{"red", "black", "blue"}

Please see that answer for more examples of use.

$\endgroup$
1
  • $\begingroup$ thanks, I somehow missed those references. My question is basically a duplicate of your first and fourth links $\endgroup$
    – glS
    Commented Feb 22, 2017 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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