Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*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 2005

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

Search the Archive

Re: match 1, replace 2

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56255] Re: match 1, replace 2
  • From: dh <dh at metrohm.ch>
  • Date: Wed, 20 Apr 2005 05:31:31 -0400 (EDT)
  • References: <d42khj$3ac$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Trevor,
it can be done, but the rule is pretty complex:

{{1, 2, 3}, {4, 5}, {6, 7, 8, 9}} /.
    {y : (_Integer ..), z_Integer} :>
    {Sequence @@ (Sequence @@ {100 + #, -1} &
     /@ List[y]), z}

May be a solution with an intermediate step is more straightforward 
here (and simplier to understand):

t = ls /. x_Integer :> {100 + x, -1}
t /. {x : ({_, _} ..), y : {_, _}} :> {x, First[y] - 100}

Sincerely, Daniel


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]?
> 
> trevor.
> 


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