# Reassign a value to an array element

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?

• use ClearAll[t,s] before making new assignments?
– kglr
Oct 14, 2018 at 1:15
• 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. Oct 14, 2018 at 2:03
• 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. Oct 14, 2018 at 2:44
• 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. Oct 14, 2018 at 5:59
• Is this not the way we should do this at all, or is it just me? Oct 14, 2018 at 8:57

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