Re: match 1, replace 2
- To: mathgroup at smc.vnet.net
- Subject: [mg56235] Re: match 1, replace 2
- From: Detlef Müller at smc.vnet.net
- Date: Wed, 20 Apr 2005 05:30:09 -0400 (EDT)
- References: <d42khj$3ac$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Trevor Baca wrote: > hi mathgroup, > > what rule takes > > ls = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}} > > to > > {{101, -1, 102, -1, 3}, {104, -1, 5}, {106, -1, 107, -1, 108, -1, > 9}} > > ie, matches all integers but the last in each list and then replaces > each matched integer n with the 2-element sequence Sequence[100 + n, > -1]? Mh, Without thinking too much, I would write: In[16]:= L={{1,2,3},{4,5},{6,7,8,9}}; Append[Drop[#,-1]/.{x_Integer->Sequence[x+100,-1]},Last[#]]&/@ L Out[17]= {{101,-1,102,-1,3},{104,-1,5},{106,-1,107,-1,108,-1,9}} Of course there should be a more elegant way for this.