MathGroup Archive 2007

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

Search the Archive

Re: ReplaceAll question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82527] Re: ReplaceAll question
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 24 Oct 2007 04:19:06 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <ffkf46$sh6$1@smc.vnet.net>

Yaroslav Bulatov wrote:

> b = 3;
> Hold[a[[b]]] /. {b -> c}
> 
> ReplaceAll has no effect, why? What is the recommended way of carrying
> out the replacement above?

After evaluation, you have Hold(a[[b]]] on the the LHS (the symbol 'b' 
has not been replaced by its value since the function Hold has been 
precisely designed to prevent it), whereas on the RHS the transformation 
rule has been evaluated to {3 -> c}. So the replacement operator cannot 
find any match between the LHS and the RHS, and consequently do nothing.

In[1]:= b = 3;
Hold[a[[b]]] /. {b -> c} // Trace

Out[2]= {{{{b, 3}, 3 -> c, 3 -> c}, {3 -> c}}, Hold[a[[b]]] /. {3 -> c}, 
Hold[a[[b]]]}

ReleaseHold seems to be the most appropriate choice to carry out the 
replacement.

In[3]:= b = 3;
Hold[a[[b]]] /. {b -> c} // ReleaseHold

During evaluation of In[3]:= Part::partd: Part specification a[[3]] \
is longer than depth of object. >>

Out[4]= a[[3]]

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: What am I doing wrong with this code?
  • Next by Date: Re: What am I doing wrong with this code?
  • Previous by thread: Re: ReplaceAll question
  • Next by thread: GraphPlot and Arrowheads