Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*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 1998

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

Search the Archive

Re: ReplacePart question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13202] Re: ReplacePart question
  • From: "Allan Hayes" <hay at haystack.demon.cc.uk>
  • Date: Mon, 13 Jul 1998 07:42:51 -0400
  • References: <6nkvd3$17k@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Here is one way round this. I give only  the full version - special
cases, for example when
expos is a single integer, or repos is omitted need extra code.

MyReplacePart[expr_, rep_, expos:{___List}, repos:{___List}]:=
Module[{f},
 MapIndexed[f, expr,{0, Infinity}, Heads->True]/.
 Thread[f[_,#]&/@expos ->Extract[rep,repos]]/.f->(#&) ]

EXAMPLE

MyReplacePart[h[a][k[b[e]],c], {A,C,{E}}, {{1,1},{1,1,1},{0,1}},
{{1},{2},{3,1}}]

h[E][k[A],c]


Allan Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642


-------------------------------------------------------------

Walter Seaman wrote in message <6nkvd3$17k at smc.vnet.net>...
>hello,
>Here is what m'ca ReplacePart  is supposed to do according to the online
>help and the manual:
>
>"ReplacePart[expr, new, pos, npos] replaces parts at positions pos in
>expr by parts at positions npos in new."
>
>For example:
>
>ReplacePart[{a, b, c,d},{x,y},{{1},{4}},2]  gives output
>{y,b,c,y}
>
>which is ok. I cannot get m'ca to replace selected parts of expr with
>selected parts of new.  For example, suppose I want to take the list
>{a,b,c,d} and replace a with y and x with b.  It seems I should be able
>to use the command
>
>ReplacePart[{a, b, c,d},{x,y},{{1},{4}},{2,1}]
>
>but the output is
>{{x,y}\[LeftDoubleBracket]2,1\[RightDoubleBracket],b,
>  c,{x,y}\[LeftDoubleBracket]2,1\[RightDoubleBracket]}
>
>i.e. m'ca is looking for {x,y}[[1,2]] (which is not defined) and trying
>to replace it in positions 1 and 4.
>If I try
>
>ReplacePart[{a, b, c,d},{x,y},{{1},{4}},{{2},{1}}]
>
>I still get the output
>
>{{x,y}\[LeftDoubleBracket]{2},{1}\[RightDoubleBracket],b,
>  c,{x,y}\[LeftDoubleBracket]{2},{1}\[RightDoubleBracket]}
>
>Can anyone tell me how to accomplish this selective replacement? Thanks,
>W.Seaman
>



  • Prev by Date: a system of equations and Solve[ ]
  • Next by Date: Problems using TWJ contour for irregular data
  • Previous by thread: Re: ReplacePart question
  • Next by thread: Re: Re: ReplacePart question