Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1993
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

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






  • Prev by Date: Re: Lists
  • Next by Date: Mathematica Help
  • Previous by thread: Re: Lists
  • Next by thread: Re: Lists