MathGroup Archive 2006

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

Search the Archive

Re: ReplacePart in an If[] construct?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67826] Re: ReplacePart in an If[] construct?
  • From: bghiggins at ucdavis.edu
  • Date: Sun, 9 Jul 2006 04:50:55 -0400 (EDT)
  • References: <e8nttf$kcf$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Your code is doing exactly what you asked it to do.
The following is a compound statement

ReplacePart[#1, replaced!, 3];ReplacePart[#1, replaced!, 2]

 and so the last part of it is returned in the If statement.

If you replace the above with

{ReplacePart[#1, replaced!, 3],ReplacePart[#1, replaced!, 2]}

The If statement will return both results:


(If[  (ListQ[#1] && First[#1] == 4),
    {ReplacePart[#1, replaced!, 3],
    ReplacePart[#1, replaced!, 2]},
   #1      (*otherwise*)
] & ) /@ testList


{{3,an,example,list},{{4,another,replaced!,list},{4,replaced!,example,list}}}


Hope this helps,

Cheers

Brian


gardyloo wrote:
> Hi, all,
>
>    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}}
>
> Out[1]=
> {{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!" ?  I have
> plenty of other ways of doing this, but this seemed the most
> straightforward, and I can't wrap my head around it, for some reason.
>
>
>
> --
> ==========================================================
> Curtis Osterhoudt
> gardyloo at mail.remove_this.wsu.and_this.edu
> PGP Key ID: 0x088E6D7A
> Please avoid sending me Word or PowerPoint attachments
> See http://www.gnu.org/philosophy/no-word-attachments.html
> ==========================================================


  • Prev by Date: Re: problems with Nmaximization
  • Next by Date: Re: failure to Integrate in orthogonal polynomials
  • Previous by thread: Re: ReplacePart in an If[] construct?
  • Next by thread: Re: ReplacePart in an If[] construct?