Re: Mixing immediate assignment and delayed assignment
- To: mathgroup at smc.vnet.net
- Subject: [mg86826] Re: Mixing immediate assignment and delayed assignment
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Sat, 22 Mar 2008 00:54:09 -0500 (EST)
- Organization: University of Bergen
- References: <frvm9c$i43$1@smc.vnet.net>
Kristian Schmidt wrote: > Hello > > I have some code akin to this: > In[261]:= list1 = Table[Null, {j, 1, 5}]; > For[i = 1, i <= 5, i++, > list1[[i]] := some_list[[i]]; > ] > > Which after its execution will have list1 filled with these references: > {some_list[[6]], some_list[[6]], some_list[[6]], some_list[[6]], > some_list[[6]]} > > But I would like the list to be filled with: > {some_list[[1]], some_list[[2]], some_list[[3]], some_list[[4]], > some_list[[5]]} > > In other words, I would like the assignment of "i" to be immediate, but the assignment of some_list to be delayed, as the list will be updated periodically. Inject the value using With[]. Also note that some_list is not a valid symbol name in Mathematica: underscores are used in patterns. In[1]:= list1 = Table[Null, {j, 5}]; In[2]:= Do[ With[{i = j}, list1[[i]] := sl[[i]] ], {j, Length[list1]} ] In[3]:= OwnValues[list1] Out[3]= {HoldPattern[list1] :> {sl[[1]], sl[[2]], sl[[3]], sl[[4]], sl[[5]]}}