MathGroup Archive 2006

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

Search the Archive

Re: ReplacePart in an Ifconstruct?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67820] Re: ReplacePart in an Ifconstruct?
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Sun, 9 Jul 2006 04:50:39 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/8/06 at 4:57 AM, gardyloo at mail.wsu.edu (gardyloo) wrote:

>It's probably that I'm too tired, but I have a question about using
>ReplacePart in an If[] construct. I've made up a (very) minimal
>example:

>In[1]:= testList = {{3, an, example, list},
>{4, another, example, list}}

>In[2]:=
>(If[  (ListQ[#1] && First[#1] == 4),
>    ReplacePart[#1, replaced!, 3];
>    ReplacePart[#1, replaced!, 2],
>   #1      (*otherwise*)
>] & ) /@ testList

>Out[2]= {{3, an, example, list}, {4, replaced!, example, list}}

>Can someone tell me why BOTH the second and third positions in
>the second element in testList aren't turned to "replaced!" ? 

You have a block expression with two operations that execute with the first element of the list is 4. The first operation replaces the third element of the list with replaced!. But since this is in a block terminated with a semicolon, you will never see this result. You will see the results of the second operation which replaces the second element with replaced.

For the test list given the conditional is exactly equivalent to doing

In[8]:=
ReplacePart[testList,replaced!,{2,3}];
ReplacePart[testList,replaced!,{2,2}]

Out[9]=
{{3, an, example, list}, {4, replaced!, example, list}}

That is the first replacement occurs but simply isn't displayed or retained in any way.
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: ReplacePart in an If[] construct?
  • Next by Date: Re: ReplacePart in an If[] construct?
  • Previous by thread: Re: help with lists operations
  • Next by thread: Re: ReplacePart in an Ifconstruct?