MathGroup Archive 2005

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

Search the Archive

Re: match 1, replace 2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56263] Re: [mg56199] match 1, replace 2
  • From: "Fred Simons" <f.h.simons at tue.nl>
  • Date: Wed, 20 Apr 2005 05:32:08 -0400 (EDT)
  • References: <200504190854.EAA02514@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Trevor,

No doubt you will see many solutions. Here there are two of them.

This one works for any list of lists:

With[{z = Last /@ ls}, 
MapThread[Append[#1, 
#2] & , 
{Replace[Most /@ ls, 
n_ :> Sequence[100 + n, 
-1], {2}], z}]]

And here is one for a list of lists of integers:

ls /. n_Integer :> Sequence[
100 + n, -1] /. 
{a___, b_, c_Integer} :> 
{a, b + c - 99}

Regards,

Fred Simons
Eindhoven University of Technology

----- Original Message ----- 
From: "Trevor Baca" <trevorbaca at gmail.com>
To: mathgroup at smc.vnet.net
Subject: [mg56263] [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.
>


  • Prev by Date: Re: plot on condition
  • Next by Date: Re: match 1, replace 2
  • Previous by thread: Re: match 1, replace 2
  • Next by thread: Re: match 1, replace 2