re: Lists
- To: mathgroup at yoda.physics.unc.edu
- Subject: re: Lists
- From: tgayley (Todd Gayley)
- Date: Fri, 29 Jan 1993 11:09:34 -0600
Neil (neilb at physics.su.oz.au) asks:
>Hi, I am try to add one to the last element of a list.
.....
>
>>In[5]:= l={x,z,y,0,0}
>>
>>Out[5]= {x, z, y, 0, 0}
>>
>>In[6]:= l/.l[[Length[l] ]]->l[[Length[l] ]]+1
>>
>>Out[6]= {x, z, y, 1, 1}
>>
>which is curious. Does anybody know why
>this doesn't produce
>{x,y,z,0,1}?
>
The substitution
l/.l[[Length[l] ]]->l[[Length[l] ]]+1
evaluates to
l /. 0->1
which explains your results. To modify a list in this way, you need to use
ReplacePart:
ReplacePart[l, Last[l]+1, Length[l]]
---------------
Todd Gayley
WRI Technical Support