Services & Resources / Wolfram Forums
MathGroup Archive
*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
  • Subject: [mg13202] Re: ReplacePart question
  • From: "Allan Hayes" <hay at>
  • Date: Mon, 13 Jul 1998 07:42:51 -0400
  • References: <6nkvd3$>
  • Sender: owner-wri-mathgroup at

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}]:=
 MapIndexed[f, expr,{0, Infinity}, Heads->True]/.
 Thread[f[_,#]&/@expos ->Extract[rep,repos]]/.f->(#&) ]


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


Allan Hayes
Training and Consulting
Leicester UK
hay at
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642


Walter Seaman wrote in message <6nkvd3$17k at>...
>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
>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
>  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
>  c,{x,y}\[LeftDoubleBracket]{2},{1}\[RightDoubleBracket]}
>Can anyone tell me how to accomplish this selective replacement? Thanks,

  • 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