I have already asked a related question here why set values in this way doesn't work? But I think I have to write the question which I encountered explicitly?
tmp = {1, 2, 3, 4, 5}
label[i_, p_] := i + p
test[p_] := Table[tmp[[label[i, p]]], {i, 1, 2}]
Here I defined a function test and the thing I want to is that change the values of list tmp via setting values to function test. While simply write
test[1]={111,222}
and expecting tmp will change to {1,111,222,3,4,5} is not possible.
So how to achive this? I have tried many Hold things, but failed.
Inspired by andre. I know that Set has attribute HoldFirst. So I tried Evaluate
Evaluate[test[1]]={111,222}
this won't work! I guess the problem is that I must properly Hold label[i,p] and tmp[[]] in the table. But I can't work it out. Anyone help?
(tmp[[1 ;; Length@#]] = #) &@{11, 22, 33}– Dr. belisarius May 03 '13 at 02:28MapIndexed[(tmp[[#2]] = #1) &, {11, 22, 33}]– Dr. belisarius May 03 '13 at 02:35