2
$\begingroup$

I have a very complex code in which I assign a value to an element of an array and then I reassign to it another one. A minimal example:

Clear[ggg];
t = {1, 2, 3};
s = {2, 5, 6};
Evaluate[ggg @@ t ] = 1;
Evaluate[ggg @@ s] = 2;

Evaluate[ggg @@ t ] = -1;
Evaluate[ggg @@ s] = 3;

and the error is

Set::setraw: Cannot assign to raw object 1.
Set::setraw: Cannot assign to raw object 2.

A solution may be to use Condition or If, but does there exist a command to force the new assignment?

$\endgroup$
10
  • $\begingroup$ use ClearAll[t,s] before making new assignments? $\endgroup$
    – kglr
    Oct 14, 2018 at 1:15
  • $\begingroup$ What are you trying to accomplish with your code? If ggg is an array, then you can use either Part or ReplacePart, and you probably do not need Evaluate at all. $\endgroup$
    – bill s
    Oct 14, 2018 at 2:03
  • $\begingroup$ ggg[Sequence @@ t] = -1; ggg[Sequence @@ s] = 3;. But I suspect that you'll want to do things a little more naturally, like make ggg a List rather than assign it DownValues as you are doing. $\endgroup$
    – march
    Oct 14, 2018 at 2:44
  • $\begingroup$ you get an error because the second time you evaluate Evaluate[ggg@@t]=-1, the left hand side of Set evaluates to 1 because you have already given it that value with the previous evaluations. Set rightly complains that it can't assign a value of -1 to 1. $\endgroup$ Oct 14, 2018 at 5:59
  • $\begingroup$ Is this not the way we should do this at all, or is it just me? $\endgroup$ Oct 14, 2018 at 8:57

1 Answer 1

3
$\begingroup$

You can use Inactive to reassign down values:

Clear[ggg];
t = {1,2,3};
s = {2,5,6};
Evaluate[ggg @@ t] = 1;
Evaluate[ggg @@ s] = 2;

Check initial values:

ggg[1,2,3]
ggg[2,5,6]

1

2

Reassign using Inactive:

Activate @ Inactive[Set][Inactive[ggg] @@ t, -1]
Activate @ Inactive[Set][Inactive[ggg] @@ s, 3]

-1

3

Check:

ggg[1,2,3]
ggg[2,5,6]

-1

3

$\endgroup$

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.