Re: match 1, replace 2
- To: mathgroup at smc.vnet.net
- Subject: [mg56256] Re: [mg56199] match 1, replace 2
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Wed, 20 Apr 2005 05:31:39 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Trevor Baca [mailto:trevorbaca at gmail.com] To: mathgroup at smc.vnet.net >Sent: Tuesday, April 19, 2005 10:55 AM >Subject: [mg56256] [mg56199] match 1, replace 2 > >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]? > >trevor. > > (Didn't appear this very same question already some time ago?) Replace[ls , {x__, y_} :> Flatten@Append[Transpose[{{x} + 100, PadLeft[{}, Length[{x}], -1]}], y], {1}] Replace[ls , {x__, y_} :> {Apply[Sequence, Transpose[{{x} + 100, PadLeft[{}, Length[{x}], -1]}], {0, 1}], y}, {1}] Replace[ls , {xx__, y_} :> Append[Replace[{xx}, x_ :> Sequence[x + 100, -1], {1}], y], {1}] Replace[Replace[ls, x_ :> Sequence[x + 100, -1], {2}], {xx__, y_, z_} :> {xx, y - 100}, {1}] etc. -- Hartmut Wolf