MathGroup Archive 2005

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

Search the Archive

Re: match 1, replace 2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56242] Re: [mg56199] match 1, replace 2
  • From: "David Park" <djmp at earthlink.net>
  • Date: Wed, 20 Apr 2005 05:30:24 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Trevor,

Here is one solution.

ls = {{1, 2, 3}, {4, 5}, {6, 7, 8, 9}};

ls /. s_?VectorQ :> Module[{first = Drop[s, -1], last = Last[s]},
      Join[Flatten[{# + 100, -1} & /@ first], {last}]]
{{101, -1, 102, -1, 3}, {104, -1, 5}, {106, -1, 107, -1, 108, -1, 9}}

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/ 


From: Trevor Baca [mailto:trevorbaca at gmail.com]
To: mathgroup at smc.vnet.net


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.





  • Prev by Date: Re: assigning functions
  • Next by Date: Re: match 1, replace 2
  • Previous by thread: Re: match 1, replace 2
  • Next by thread: Re: match 1, replace 2