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: rules and lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54247] Re: rules and lists
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Mon, 14 Feb 2005 21:50:40 -0500 (EST)
  • References: <cup65m$e04$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Erich Neuwirth wrote:
> I would like to replace sublists of a list to be replaced by another 
> list, such that for example the rule
> {1,2}->{1,2,3}
> wout tranform
> {1,2,3,3,1,2} into
> {1,2,3,3,3,1,2,3}
> Simply gibbin the transformation rule in the form
> {x___,1,2,y___}->{x,1,2,3,y}
> does not work.
> What is the idiom to do what I want?
> 
This is slightly more subtle than it looks. The point is that /. will 
match your whole list with the pattern you have used - so it will only 
find one instance of your sublist. However, if you use //. you will get 
an infinite loop. The easiest way is to use a transformation that 
replaces each sublist with symbols - so you can use //. and match all 
the sublists, then replace the symbols with their proper values.

{1,2,3,3,1,2}//.{x___,1,2,y___}->{x,a1,a2,a3,y} /. {a1->1,a2->2,
     a3->3}

{1, 2, 3, 1, 2, 3, 2}

David Bailey
dbaileyconsultancy.co.uk


  • Prev by Date: Re: Using Select with arrays? (Relative newbie)
  • Next by Date: Simplifying an expression in light of relationships between variables?
  • Previous by thread: Re: rules and lists
  • Next by thread: Re: rules and lists